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"
Hint:
- Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
- Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
- There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
链接: http://leetcode.com/problems/integer-to-english-words/
题解:
把数字翻译成英文。这个跟Integer to Roman很像,把情况分清楚就不难解决。代码大都参考了Discuss里hwy_2015的,简洁易懂。主要是以1000为一个单位来把数组分成组,每个组内单独处理tens和lessThanTwenty的情况。
Time Complexity - O(n), Space Complexity - O(n)。
public class Solution {
private final String[] lessThanTwenty = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private final String[] thousands = {"" ,"Thousand", "Million", "Billion"}; public String numberToWords(int num) {
if(num <= 0) {
return "Zero";
}
String words = "";
int index = 0; while(num > 0) {
if(num % 1000 != 0) {
words = getNum(num % 1000) + thousands[index] + " " + words;
}
num /= 1000;
index++;
} return words.trim();
} private String getNum(int num) {
if(num <= 0) {
return "";
} else if (num < 20) {
return lessThanTwenty[num] + " ";
} else if (num < 100) {
return tens[num / 10] + " " + getNum(num % 10);
} else {
return lessThanTwenty[num / 100] + " Hundred " + getNum(num % 100);
}
}
}
Reference:
https://leetcode.com/discuss/55462/my-clean-java-solution-very-easy-to-understand
https://leetcode.com/discuss/55349/if-you-know-how-to-read-numbers-you-can-make-it
https://leetcode.com/discuss/71544/short-clean-java-solution
https://leetcode.com/discuss/60010/share-my-clean-java-solution
https://leetcode.com/discuss/55273/my-java-solution
https://leetcode.com/discuss/55477/recursive-python
273. Integer to English Words的更多相关文章
- leetcode-【hard】273. Integer to English Words
题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...
- 【LeetCode】273. Integer to English Words
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- 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 ...
- 273. Integer to English Words数字转为单词
[抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
- [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 ...
- [LC] 273. Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- 273 Integer to English Words 整数转换英文表示
将非负整数转换为其对应的英文表示,给定的输入是保证小于 231 - 1 的.示例:123 -> "One Hundred Twenty Three"12345 -> & ...
随机推荐
- Analyzer使用第二Y轴,以及同一分析图不同量值使用不同的图形样式
Analyzer的建立分析图后,图中有两个量值,希望能显示成不同的图形样式,如一个是柱图.一个是线图. 1.设置显示多个量值: 3.设置显示出图例,即表明图中量值内容的说明: 2.右键图例中要修改为不 ...
- python 关键字参数
原文地址:http://docs.pythontab.com/python/python3.4/controlflow.html#tut-functions 函数可以通过 关键字参数 的形式来调用,形 ...
- SASS学习笔记_01
scss两种格式 sass 大括号 scss css写法 Arguments: --no-cache –style compressed --update $FileName$:c ...
- 02.Redis主从集群的Sentinel配置
1.集群环境 1.Linux服务器列表 使用4台CentOS Linux服务器搭建环境,其IP地址如下: 192.168.110.100 192.168.110.101 192.168.110.102 ...
- hdu 3123 GCC 阶乘
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123 The GNU Compiler Collection (usually shortened t ...
- 【BZOJ】【1030】【JSOI2007】文本生成器
AC自动机/DP Orz ZYF 玛雅快要省选了,赶紧复(xue)习(xi)一下AC自动机…… 其实在AC自动机上DP并没有当初想的那么复杂……就是把DP的转移关系换成了AC自动机上的边而已(不过这题 ...
- 2010 Asia Fuzhou Regional Contest
A hard Aoshu Problem http://acm.hdu.edu.cn/showproblem.php?pid=3699 用深搜写排列,除法要注意,还有不能有前导零.当然可以5个for, ...
- 手写PE文件(一)
DOS Header(IMAGE_DOS_HEADER)->64 Byte DOS头部 DOS Stub 112字节 "PE"00(Signature) 4个字节 IMAGE ...
- [Bug]没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限
问题 环境WIN8.1 x64,新安装的vs与iis,在部署网站时,出现该异常信息.本地机作为测试机,就部署一个站点进行测试,没想到出现这个错误. 解决方案 如果你访问上面的那个路径,你会发现并没有那 ...
- asp.net MVC3 + JQuery 的ajax简单使用
一直都没有使用过JQuery,更没使用过JQuery的ajax支持带来的方便,今天试了一下,真是减少了很多工作量,使用方法也比较简单 这里先记下来,以后使用时可以再拿着用. 本应用中,本来是准备使用长 ...