📌  相关文章
📜  将给定的整数数组转换为单词

📅  最后修改于: 2022-05-13 01:57:07.707000             🧑  作者: Mango

将给定的整数数组转换为单词

给定一个数组arr[] ,包含范围 [0, 9] 内的N个元素。任务是将每个数组元素转换为其数字字符串。

例子:

方法:可以借助地图解决问题。请按照以下步骤操作:

  • 将每个数字从 1 到 9 映射到它们各自的数字字符串。
  • 遍历数组并将每个数字更改为它映射到的字符串。

下面是上述方法的实现。

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to convert
// the numeric array to words
void convert(int arr[], int N)
{
    map mp;
    int i;
 
    // Map the integers to
    // their respective numeric  string
    mp[0] = "zero";
    mp[1] = "one";
    mp[2] = "two";
    mp[3] = "three";
    mp[4] = "four";
    mp[5] = "five";
    mp[6] = "six";
    mp[7] = "seven";
    mp[8] = "eight";
    mp[9] = "nine";
 
    // Traverse array elements and print
    for (i = 0; i < N; i++)
        cout << mp[arr[i]] << " ";
 
    cout << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 4, 3, 2, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    convert(arr, N);
    return 0;
}


Java
// JAVA code to implement the approach
import java.util.*;
class GFG {
 
  // Function to convert
  // the numeric array to words
  public static void convert(int arr[], int N)
  {
    HashMap mp = new HashMap<>();
    int i;
 
    // Map the integers to
    // their respective numeric  string
    mp.put(0, "zero");
    mp.put(1, "one");
    mp.put(2, "two");
    mp.put(3, "three");
    mp.put(4, "four");
    mp.put(5, "five");
    mp.put(6, "six");
    mp.put(7, "seven");
    mp.put(8, "eight");
    mp.put(9, "nine");
 
    // Traverse array elements and print
    for (i = 0; i < N; i++)
      System.out.print(mp.get(arr[i]) + " ");
 
    System.out.println();
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = new int[] { 1, 4, 3, 2, 6 };
    int N = arr.length;
 
    // Function call
    convert(arr, N);
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for the above approach
# Function to convert
# the numeric array to words
def convert(arr, N):
    mp = {};
     
    # Map the integers to
    # their respective numeric string
    mp[0] = "zero";
    mp[1] = "one";
    mp[2] = "two";
    mp[3] = "three";
    mp[4] = "four";
    mp[5] = "five";
    mp[6] = "six";
    mp[7] = "seven";
    mp[8] = "eight";
    mp[9] = "nine";
 
     # Traverse array elements and print
    for i in range(N):
        print(mp[arr[i]], end = " ");
 
# Driver Code
arr = [1, 4, 3, 2, 6];
N = len(arr);
 
# Function call
convert(arr, N);
 
# This code is contributed by Potta Lokesh


C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to convert
  // the numeric array to words
  static void convert(int []arr, int N)
  {
    Dictionary mp = 
      new Dictionary();
    int i;
 
    // Map the integers to
    // their respective numeric  string
    mp[0] = "zero";
    mp[1] = "one";
    mp[2] = "two";
    mp[3] = "three";
    mp[4] = "four";
    mp[5] = "five";
    mp[6] = "six";
    mp[7] = "seven";
    mp[8] = "eight";
    mp[9] = "nine";
 
    // Traverse array elements and print
    for (i = 0; i < N; i++)
      Console.Write(mp[arr[i]] + " ");
 
    Console.WriteLine();
  }
 
  // Driver Code
  public static void Main()
  {
    int []arr = { 1, 4, 3, 2, 6 };
    int N = arr.Length;
 
    // Function call
    convert(arr, N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
one four three two six 

时间复杂度: O(N)。
辅助空间: O(N)。