leetcode-【hard】273. Integer to English Words
题目:
273. Integer to English Words
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、这道题目不是难在思路上,而是难在考虑的情况比较多,比较多细节,一不小心就有小bug,需要思路清晰,逻辑清晰
2、英文中用来表示整数的单词并不多,分类:
1-9(个位数),11-19(特殊的十位数),10-90(十位数),100(hundred),1000(thousand),1000000(million),1000000000(billion)
3、10这个十位数比较特别,只有在后两位完全为10时,才用ten
4、注意空格,后面没有数字了,就不能加空格了
5、每千位数跟后面的千位数(如果不为空,即0000000……)要有空格
6、注意不要乱加前缀空格和后缀空格
将上面的细节注意了就AC啦
代码:
#include <vector>
#include <string> using std::vector;
using std::string; string n2s[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
string g2s[] = {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
string t2s[] = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; class Solution {
private:
vector<string> ans; public:
string numberToWords(int num) {
if(num == )
{
return "Zero";
} unsigned int rem;
unsigned int one;
unsigned int two;
unsigned int three; string perAns;
ans.clear(); do
{
//获取余数
rem = num % ;
num = num /; //存储每次计算的结果
perAns.clear();
perAns = ""; //一千以内的数,取每一位
one = rem % ;
two = (rem / ) % ;
three = (rem / ) % ; if(three != )
{
perAns += n2s[three - ];
perAns += " Hundred";
} //如果后两位都为0,那么后面就没有数了,也就不需要加空格
if(perAns != "" && (one != || two != ))
{
perAns += " ";
} if(two == )
{
//考虑最后一位是否为0的情况,因最后两位为10时,需要用“ten”
//否则就是十位数
if(one == )
{
perAns += t2s[];
}else
{
perAns += g2s[one - ];
}
//考虑two不为0,不为1,则按一般规则去计算
}else if(two != )
{
perAns += t2s[two - ]; if(one != )
{
perAns += " ";//如果最后一位不为0,需要在其前面加空格
perAns += n2s[one - ];
}
//考虑two为0的情况
}else
{
if(one != )
{
perAns += n2s[one - ];
}
} //将结果存储到ans中,ans中的答案是以逆序形式存储了每个千位数
ans.push_back(perAns);
}while(num != ); string result = "";
unsigned int len = ans.size();
//len最大长度为4,考虑每种位数就行,这里用的时候就会发现,合理使用goto,程序逻辑会很清晰
switch(len)
{
case :goto three;break;
case :goto two;break;
case :goto one;break;
case :goto zero;break;
} three:
if(ans[] != "")
{
result += ans[];
result += " Billion"; if(ans[] != "" || ans[] != "" || ans[] != "")
{
result += " ";
}
} two:
if(ans[] != "")
{
result += ans[];
result += " Million"; if(ans[] != "" || ans[] != "")
{
result += " ";
}
} one:
if(ans[] != "")
{
result += ans[];
result += " Thousand"; if(ans[] != "")
{
result += " ";
}
} zero:
if(ans[] != "")
{
result += ans[];
} return result;
}
};
说明: 合理使用goto会取到很好的效果哦
leetcode-【hard】273. Integer to English Words的更多相关文章
- 【LeetCode】273. Integer to English Words
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- LeetCode: 【L4】N-Queens 解题报告
[L4]N-Queens 解题报告 N-Queens Total Accepted: 16418 Total Submissions: 63309 My Submissions The n-queen ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- LeetCode 273. Integer to English Words
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/description/ 题目: Convert a non-negati ...
- 【LeetCode算法-7】Reverse Integer
LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...
- [leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- 【Leetcode】【Easy】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
随机推荐
- Android——播放器和图片轮播
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- ASP.NET Razor - C# 循环和数组
语句在循环中会被重复执行. For 循环 如果您需要重复执行相同的语句,您可以设定一个循环. 如果您知道要循环的次数,您可以使用 for 循环.这种类型的循环在向上计数或向下计数时特别有用: 实例 & ...
- JS在路径中传中文参数
需要用 encodeURI('中文');处理一下.
- IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic、 @synthesize、@property、@dynamic
IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic. @synth ...
- C# 导入Excel到DataSet中
class Import { /// <summary> /// 导入Excel到DataSet中 /// </summary> /// <param name=&quo ...
- 时间戳 JavaScript parse() 方法 处理技巧
返回1970/01/01至2012/3/21之间的毫秒数: var d = Date.parse("March 21, 2012"); d 输出结果: 1332259200000 ...
- Nginx和Apache配置日志格式记录Cookie
记录Cookie有什么用? 有时候我们需要通过web服务器的访问日志来统计UV(独立访客),并据此分析用户的行为.而UV是依据cookie数据得出的统计.UV相对于IP的好处是:IP是一个反映网络虚拟 ...
- CentOS 6 到 CentOS 7
redhat提供了一个redhat-upgrade-tool的升级工具: [1]配置软件源 # vim /etc/yum.repos.d/upgrade.repo [upgrade] name=u ...
- java编译期优化
java语言的编译期其实是一段不确定的操作过程,因为它可以分为三类编译过程: 1.前端编译:把.java文件转变为.class文件 2.后端编译:把字节码转变为机器码 3.静态提前编译:直接把*.ja ...
- SpringMVC 请求路径结尾存在.使用@PathVariable访问路径内容,路径参数获取不准确的问题
SpringMVC采用Get方式请求资源时,如果请求路径的结尾中带有小数点(.)时,同时使用@PathVariable访问路径内容时,请求路径中最后一个小数点及其后面的内容会被Spring截断丢弃比如 ...