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.
Example 1:
Input: 123
Output: "One Hundred Twenty Three"
Example 2:
Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
个位数是0的时候不用说zero,直接说英文名就行了
所以<10数组的第一位是“”
[思维问题]:
不知道怎么把数字分开,/ k % k就行了,但是这道题的重点并不是把数分开,而是如何处理10、20、100内有英文名的特殊数字以及hundred thousand million billion
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
“英文名” + 每次都新建字符串的helper函数连接一下就行了
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
可能出现空格字符串啊,所以都先赋值给result, 最后再统一string.trim一下
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
记住10、20、100内有英文名的特殊数字以及hundred thousand million billion 才有英文名
helper函数中每次都新建字符串,才能和一般字符串用+连接
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
public class Solution {
private final String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private final String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] belowHundred = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String numberToWords(int num) {
if (num == 0) return "Zero";
return helper(num);
}
private String helper(int num) {
String result = new String();
if (num < 10) result = belowTen[num];
else if (num < 20) result = belowTwenty[num -10];
else if (num < 100) result = belowHundred[num/10] + " " + helper(num % 10);
else if (num < 1000) result = helper(num/100) + " Hundred " + helper(num % 100);
else if (num < 1000000) result = helper(num/1000) + " Thousand " + helper(num % 1000);
else if (num < 1000000000) result = helper(num/1000000) + " Million " + helper(num % 1000000);
else result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
return result.trim();
}
}
273. Integer to English Words数字转为单词的更多相关文章
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- leetcode-【hard】273. Integer to English Words
题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...
- [LeetCode] 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
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- 273. Integer to English Words
题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...
- [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@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [LC] 273. Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
随机推荐
- Arrays--codility
lesson 2: Arrays exercise: Problem: Given array A consisting of N integers, return the reversed arra ...
- Android照片墙完整版,完美结合LruCache和DiskLruCache
转载地址:http://blog.csdn.net/guolin_blog/article/details/34093441#comments 在上一篇文章当中,我们学习了DiskLruCache的概 ...
- 【备忘录】Sublime Text编辑器如何在选中的多行行首增加字符串
如题:上面的代码,想在每一行的开头加上一个字符 * 如下: 操作步骤如下: 1.选中要操作的行(我这里Ctrl+A) 2.Ctrl+Shift+L (待操作状态) 3.方向键← (操作这步骤后,可 ...
- Google Cloud IAM中添加自定义域名
Google Cloud支持在IAM中加入自定义域名.具体方法如下: 1. 登录Google Cloud Console,点击IAM和管理,进入身份和组织 2. 点击注册 3. 会重定向到Gmail到 ...
- JVM内存管理之GC算法精解(复制算法与标记/整理算法)
本次LZ和各位分享GC最后两种算法,复制算法以及标记/整理算法.上一章在讲解标记/清除算法时已经提到过,这两种算法都是在此基础上演化而来的,究竟这两种算法优化了之前标记/清除算法的哪些问题呢? 复制算 ...
- Range(转)
原文链接:http://www.cnblogs.com/peida/p/Guava_Range.html 在Guava中新增了一个新的类型Range,从名字就可以了解到,这个是和区间有关的数据结构.从 ...
- h5调用手机摄像头/相册
<!DOCTYPE HTML><html><head> <title>上传图片</title> <meta charset=" ...
- 学生党如何拿到阿里技术offer: 《2016阿里巴巴校招内推offer之Java研发工程师(成功)》
摘要: 这篇文章字字珠玑,这位面试的学长并非计算机相关专业,但是其技术功底足以使很多计算机专业的学生汗颜,这篇文章值得我们仔细品读,其逻辑条理清晰,问题把握透彻,语言表达精炼,为我们提供了宝贵的学习经 ...
- line 3: /usr/local/arm/4.3.2/bin/arm-none-linux-gnueabi-gcc: No such file or directory
sudo apt-get install lib32ncurses5(网上下载的很多arm-linux-gcc都是32位的,64位的ubuntu需要按此包)
- TIMEQUEST学习之黑金动力(四)
现在知道时序约束主要是FPGA to ic,或者ic to FPGA. 上图可以表示FPGA to IC, IC to FPGA. fpga2ic:fpga2ext 是 fpga 致 ic 信号的走线 ...