leetCode 273. Integer to English Words 字符串 | Hard

简介:

273. Integer to English Words


Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

题目大意:

将一个数字转换成它的英文读法。

思路:

1.将数字以3位为一组,分组。

2.组合每组的字符串。

3.将每组字符串和自己的单位结合起来。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class  Solution {
public :
 
     string& trim(string &s)  //C++ 去字符串两边的空格
     {
         if  (s.empty()) 
         {
             return  s;
         }
         s.erase(0,s.find_first_not_of( " " ));
         s.erase(s.find_last_not_of( " " ) + 1);
         return  s;
     }
     string numberToWords( int  num) {
         
         if (num == 0)
             return  "Zero" ;
         string units_pre20[20] = { "" , "One" , "Two" , "Three" , "Four" , "Five" ,
                 "Six" , "Seven" , "Eight" , "Nine" , "Ten" ,
                 "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" ,
                 "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" };
         string units_10[10] = { "" , "" , "Twenty" , "Thirty" , "Forty" , "Fifty" ,
                       "Sixty" , "Seventy" , "Eighty" , "Ninety" };
 
         string units[4] = { "" , "Thousand" , "Million" , "Billion" };
 
         vector< int  > temp;
         int  record = num;
         
         string result;
         while (record > 0) //3位分段
         {
             temp.push_back(record % 1000);
             record /= 1000;
         }
         
         for ( int  i = 0; i < temp.size(); i++) //每段处理
         {
             string tmpStr;
             int  slices = temp[i];
             int  nHundreds = 0;
             int  nTens = 0;
             int  nUnits = 0;
             if (slices == 0)
                 continue ;
             if (slices >= 100)
             {
                 nHundreds = slices / 100;
                 tmpStr = tmpStr + units_pre20[nHundreds] +  " Hundred" ;
                 slices = slices % 100;
             }
             
             if (slices >= 20)
             {
                 tmpStr = tmpStr +  " "  + units_10[slices / 10] ;
                 if (slices % 10 != 0)
                 {
                     tmpStr = tmpStr +  " "  + units_pre20[slices % 10];
                 }
                 
             } else  if (slices < 20 && slices > 0)
             {
                 tmpStr = tmpStr +  " "  + units_pre20[slices];
             }
             
             if (i != 0)
             {
                 tmpStr = tmpStr +  " "  + units[i];
             }
             
             result = tmpStr +  " "  + result;
             trim(result);
         }
         return  result;
     }
};

总结:

熟练度还是低,一个成熟的想法,需要半个多小时去实现。。。

呵呵,好吧,继续练习。



本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1840376

相关文章
|
2月前
|
Go C++
【力扣】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
【2月更文挑战第18天】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
34 6
|
2月前
|
存储
力扣面试经典题之数组/字符串
力扣面试经典题之数组/字符串
23 0
|
12天前
[leetcode~数位动态规划] 2719. 统计整数数目 hard
[leetcode~数位动态规划] 2719. 统计整数数目 hard
|
17天前
|
算法
代码随想录算法训练营第五十五天 | LeetCode 583. 两个字符串的删除操作、72. 编辑距离、编辑距离总结
代码随想录算法训练营第五十五天 | LeetCode 583. 两个字符串的删除操作、72. 编辑距离、编辑距离总结
24 1
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
20天前
|
机器学习/深度学习 索引
【力扣】387. 字符串中的第一个唯一字符
【力扣】387. 字符串中的第一个唯一字符
|
2月前
|
存储
leetcode2744. 最大字符串配对数目
leetcode2744. 最大字符串配对数目
17 0
|
2月前
|
机器学习/深度学习 NoSQL Shell
力扣刷题-翻转字符串
力扣刷题-翻转字符串
12 1
|
2月前
|
算法 Java
[Java·算法·简单] LeetCode 28. 找出字符串中第一个匹配项的下标 详细解读
[Java·算法·简单] LeetCode 28. 找出字符串中第一个匹配项的下标 详细解读
23 0
|
2月前
|
Go C++
【力扣】2645. 构造有效字符串的最小插入数(动态规划 贪心 滚动数组优化 C++ Go)
【2月更文挑战第17天】2645. 构造有效字符串的最小插入数(动态规划 贪心 滚动数组优化 C++ Go)
31 8

热门文章

最新文章