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
。。。
1 class Solution(object): 2 def numberToWords(self, num): 3 """ 4 :type num: int 5 :rtype: str 6 """ 7 if num == 0: 8 return "Zero" 9 less_then_twenty_words = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] 10 ty_words = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] 11 dex_words = ["Billion", "Million", "Thousand", "Hundred"] 12 radix = [1000000000, 1000000, 1000, 100] 13 s = "" 14 for i in range(len(radix)): 15 count = num / radix[i] 16 if count == 0: 17 continue 18 s = s + self.numberToWords(count) + " " + dex_words[i] + " " 19 num = num % radix[i] 20 if num < 20: 21 s = s + less_then_twenty_words[num] 22 elif num < 100: 23 s = s + ty_words[num / 10 - 2] + " " + less_then_twenty_words[num % 10] 24 return s.strip() 25
Similar way, but more easy understanding codes.
1 public class Solution { 2 private Map<Integer, String> map = new HashMap<>(); 3 4 public String numberToWords(int num) { 5 fillMap(); 6 if (num == 0) { 7 return map.get(0); 8 } 9 List<String> list = new ArrayList<>(); 10 StringBuilder sb = new StringBuilder(); 11 if (num >= 1000000000) { 12 int extra = num / 1000000000; 13 list.addAll(convert(extra)); 14 list.add("Billion"); 15 num = num % 1000000000; 16 } 17 18 if (num >= 1000000) { 19 int extra = num / 1000000; 20 list.addAll(convert(extra)); 21 list.add("Million"); 22 num = num % 1000000; 23 } 24 25 if (num >= 1000) { 26 int extra = num / 1000; 27 list.addAll(convert(extra)); 28 list.add("Thousand"); 29 num = num % 1000; 30 } 31 32 if (num >= 100) { 33 int extra = num / 100; 34 list.addAll(convert(extra)); 35 list.add("Hundred"); 36 num = num % 100; 37 } 38 39 if (num > 0) { 40 list.addAll(convert(num)); 41 } 42 43 for (int i = 0; i < list.size(); i++) { 44 sb.append(list.get(i) + " "); 45 } 46 47 return sb.toString().trim(); 48 } 49 50 private List<String> convert(int x) { 51 List<String> list = new ArrayList<>(); 52 if (x >= 100) { 53 int num = x / 100; 54 list.add(map.get(num)); 55 list.add("Hundred"); 56 x = x % 100; 57 } 58 59 if (x > 0) { 60 if (x > 20) { 61 int remainder = x % 10; 62 x = x - remainder; 63 list.add(map.get(x)); 64 if (remainder > 0) { 65 list.add(map.get(remainder)); 66 } 67 } else { 68 list.add(map.get(x)); 69 } 70 } 71 return list; 72 } 73 74 private void fillMap() { 75 map.put(0, "Zero"); 76 map.put(1, "One"); 77 map.put(2, "Two"); 78 map.put(3, "Three"); 79 map.put(4, "Four"); 80 map.put(5, "Five"); 81 map.put(6, "Six"); 82 map.put(7, "Seven"); 83 map.put(8, "Eight"); 84 map.put(9, "Nine"); 85 map.put(10, "Ten"); 86 map.put(11, "Eleven"); 87 map.put(12, "Twelve"); 88 map.put(13, "Thirteen"); 89 map.put(14, "Fourteen"); 90 map.put(15, "Fifteen"); 91 map.put(16, "Sixteen"); 92 map.put(17, "Seventeen"); 93 map.put(18, "Eighteen"); 94 map.put(19, "Nineteen"); 95 map.put(20, "Twenty"); 96 map.put(30, "Thirty"); 97 map.put(40, "Forty"); 98 map.put(50, "Fifty"); 99 map.put(60, "Sixty"); 100 map.put(70, "Seventy"); 101 map.put(80, "Eighty"); 102 map.put(90, "Ninety"); 103 } 104 }