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 ...
随机推荐
- hdu5024-Wang Xifeng's Little Plot
此题一开始用暴力做,后来发现斜着走的时候其实暴力不太好写,于是改用搜索写了 #include <iostream> #include <stdio.h> #include &l ...
- <转载>无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
转载http://blog.sina.com.cn/s/blog_6e6c5f230100p92p.html 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引 ...
- InsertSort 插入排序
插入排序:将下一个插入已排好的序列中 自己觉得演示的号的一个文章地址 http://sjjg.js.zwu.edu.cn/SFXX/sf1/zjcr.html 下面是java的实现代码: //Inse ...
- Java中BigDecimal的8种舍入模式是怎样的
Java中BigDecimal的8种舍入模式是怎样的?下面长沙欧柏泰克软件学院和大家一起来学习下吧: java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecima ...
- Appium 一个测试套件多次启动android应用
AppiumDriver<WebElement> driver; File classpathRoot = new File(System.getProperty("user.d ...
- C++简介
本文仅用于学习交流,转载请注明:http://www.cnblogs.com/mxbs/p/6266466.html Hello,C++ World! 简介: C++融合了3中不同的编程传统:C语言 ...
- JS时间操作
/** * 判断年份是否为润年 * * @param {Number} year */ function isLeapYear(year) { return (year % 400 == 0) || ...
- ORACLE SEQUENCE 介绍
在oracle中sequence就是所谓的序列号,每次取的时候它会自己主动添加,一般用在须要按序列号排序的地方. 1.Create Sequence 你首先要有CREATE SEQUENCE或者C ...
- Unity发送短信
闲来无事,觉得用uinity来发送短信挺有意思的,所以自己差了点资料,看看能否实现,结果还真的可以!废话不多说,直接码! 1,新建一空工程,我们就简单的使用UGUI搭建一个丑陋的界面吧! 2,界面极其 ...
- android 新浪微博客户端的表情功能的实现
这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片 ...