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");
}
}