Integer to English Words 解答
Question
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)
Solution
根据英文表示数字的规则,这题的关键在于三位三位的处理数字。
小于100的数:1. 小于20的,没有什么规律,只能存到数组里 2. 大于等于20的,可以按十位和个位拆解。
如 23,345,100,089
先和billion(1,000,000,000)比较,除法得到23
再和million(1,000,000)比较,除法得到345,345再和hundred(100)比较,得到3和45
。。。
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0:
return "Zero"
less_then_twenty_words = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
ty_words = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
dex_words = ["Billion", "Million", "Thousand", "Hundred"]
radix = [1000000000, 1000000, 1000, 100]
s = ""
for i in range(len(radix)):
count = num / radix[i]
if count == 0:
continue
s = s + self.numberToWords(count) + " " + dex_words[i] + " "
num = num % radix[i]
if num < 20:
s = s + less_then_twenty_words[num]
elif num < 100:
s = s + ty_words[num / 10 - 2] + " " + less_then_twenty_words[num % 10]
return s.strip()
Similar way, but more easy understanding codes.
public class Solution {
private Map<Integer, String> map = new HashMap<>(); public String numberToWords(int num) {
fillMap();
if (num == 0) {
return map.get(0);
}
List<String> list = new ArrayList<>();
StringBuilder sb = new StringBuilder();
if (num >= 1000000000) {
int extra = num / 1000000000;
list.addAll(convert(extra));
list.add("Billion");
num = num % 1000000000;
} if (num >= 1000000) {
int extra = num / 1000000;
list.addAll(convert(extra));
list.add("Million");
num = num % 1000000;
} if (num >= 1000) {
int extra = num / 1000;
list.addAll(convert(extra));
list.add("Thousand");
num = num % 1000;
} if (num >= 100) {
int extra = num / 100;
list.addAll(convert(extra));
list.add("Hundred");
num = num % 100;
} if (num > 0) {
list.addAll(convert(num));
} for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i) + " ");
} return sb.toString().trim();
} private List<String> convert(int x) {
List<String> list = new ArrayList<>();
if (x >= 100) {
int num = x / 100;
list.add(map.get(num));
list.add("Hundred");
x = x % 100;
} if (x > 0) {
if (x > 20) {
int remainder = x % 10;
x = x - remainder;
list.add(map.get(x));
if (remainder > 0) {
list.add(map.get(remainder));
}
} else {
list.add(map.get(x));
}
}
return list;
} private void fillMap() {
map.put(0, "Zero");
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
map.put(4, "Four");
map.put(5, "Five");
map.put(6, "Six");
map.put(7, "Seven");
map.put(8, "Eight");
map.put(9, "Nine");
map.put(10, "Ten");
map.put(11, "Eleven");
map.put(12, "Twelve");
map.put(13, "Thirteen");
map.put(14, "Fourteen");
map.put(15, "Fifteen");
map.put(16, "Sixteen");
map.put(17, "Seventeen");
map.put(18, "Eighteen");
map.put(19, "Nineteen");
map.put(20, "Twenty");
map.put(30, "Thirty");
map.put(40, "Forty");
map.put(50, "Fifty");
map.put(60, "Sixty");
map.put(70, "Seventy");
map.put(80, "Eighty");
map.put(90, "Ninety");
}
}
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】Integer to English Words 解题报告
Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- LeetCode Integer to English Words
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to it ...
- 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 (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [Swift]LeetCode273. 整数转换英文表示 | Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- Integer to English words leetcode java
问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
随机推荐
- 旋的X-Di
旋的X-Di | 氪加 旋的X-Di
- ubuntu 14.04下练习lua
随着lua越来越成熟,在服务器中应用也越来越广.自己也想向这方面发展,于是便开始lua的学习. 学习新的语言,应该是先编译.安装.部署开发调试环境,然后练习...可是,我现在并没有项目做啊,我只是想先 ...
- HDU2111 Saving HDU 【贪心】
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- MongoDB分片技术[转]
8天学通MongoDB——第六天 分片技术 在mongodb里面存在另一种集群,就是分片技术,跟sql server的表分区类似,我们知道当数据量达到T级别的时候,我们的磁盘,内存 就吃不消了,针 ...
- 正确的安装qwtplot3D开发库
1.从网上下载qwtplot3D的最新版本:http://qwtplot3d.sourceforge.net/ 2.解压qwtplot3d-0.2.7.zip到C盘根目录下(注意:路径中不能带有中文汉 ...
- Qt 图形特效(Graphics Effect)介绍
原文链接:Qt 图形特效(Graphics Effect)介绍 QGraphicsEffect也是Qt-4.6引入的一个新功能.它让给图形元素QGraphicsItem增加更佳视觉效果的编程变得非常简 ...
- Docker的简单认知
Docker images: docker image是一个只读打模板,用来创建Docker 容器 Docker Registers 互联网上存储images的地方 Docker containers ...
- C#通过生成ini文件,记住用户关闭程序之前的选择+忽略跨线程检查
1.在类的里面添加 //写配置文件 [DllImport("kernel32")] private static extern long WritePrivateProfileSt ...
- 一个经试用效果非常不错的数据库连接池--JAVA
前言: 虽说现在许多企业级的应用服务器均自己带有数据库连接池功能,就连 Tomcat 也支持了这种功能.然而在许多时候,我们还是要使用数据库连接池,如:访问数据库的 Java 桌面应用程序等.这个数据 ...
- django post方法不能提交
def login(request): if request.method == 'GET': c = {} c.update(csrf(request)) return render_to_resp ...