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

相关文章
|
5月前
|
Go 索引
【LeetCode 热题100】394:字符串解码(详细解析)(Go语言版)
本文详细解析了 LeetCode 热题 394:字符串解码。题目要求对编码字符串如 `k[encoded_string]` 进行解码,其中 `encoded_string` 需重复 `k` 次。文章提供了两种解法:使用栈模拟和递归 DFS,并附有 Go 语言实现代码。栈解法通过数字栈与字符串栈记录状态,适合迭代;递归解法则利用函数调用处理嵌套结构,代码更简洁。两者时间复杂度均为 O(n),但递归需注意栈深度问题。文章还总结了解题注意事项及适用场景,帮助读者更好地掌握字符串嵌套解析技巧。
140 6
|
6月前
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
186 11
|
11月前
|
JavaScript
力扣3333.找到初始输入字符串Ⅱ
【10月更文挑战第9天】力扣3333.找到初始输入字符串Ⅱ
114 1
|
11月前
|
C++
Leetcode第43题(字符串相乘)
本篇介绍了一种用C++实现的字符串表示的非负整数相乘的方法,通过逆向编号字符串,将乘法运算转化为二维数组的累加过程,最后处理进位并转换为字符串结果,解决了两个大数相乘的问题。
75 9
|
11月前
|
算法 C++
Leetcode第八题(字符串转换整数(atoi))
这篇文章介绍了LeetCode上第8题“字符串转换整数(atoi)”的解题思路和C++的实现方法,包括处理前导空格、正负号、连续数字字符以及整数溢出的情况。
123 0
|
11月前
【LeetCode 22】459.重复的子字符串
【LeetCode 22】459.重复的子字符串
93 0
|
11月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
74 0
|
12月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
177 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
133 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
293 2