1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| class Solution { public String numberToWords(int num) { String str = String.valueOf(num);
String firstPart = null, midPart = null; String lastPart = str.substring(Math.max(0, str.length() - 3)); if (str.length() > 3) { midPart = str.substring(Math.max(0, str.length() - 6), str.length() - 3); } if (str.length() > 6) { firstPart = str.substring(0, str.length() - 6); }
List<String> wordList = new ArrayList<>(); if (firstPart != null) { List<String> firstWordList = toWordList(firstPart); if (!firstWordList.isEmpty()) { wordList.addAll(firstWordList); if (!firstWordList.get(firstWordList.size() - 1).equals("Billion")) { wordList.add("Million"); } } } if (midPart != null) { List<String> midWordList = toWordList(midPart); if (!midWordList.isEmpty()) { wordList.addAll(midWordList); wordList.add("Thousand"); } } wordList.addAll(toWordList(lastPart));
return String.join(" ", wordList); }
private List<String> toWordList(String part) { if (part.length() == 1) { return Collections.singletonList(charToWord(part.charAt(0))); } else if (part.length() == 2) { return twoCharToWord(part.charAt(0), part.charAt(1)); } else if (part.length() == 3) { List<String> wordList = new ArrayList<>(); char c1 = part.charAt(0); if (c1 != '0') { wordList.add(charToWord(part.charAt(0))); wordList.add("Hundred"); } wordList.addAll(twoCharToWord(part.charAt(1), part.charAt(2))); return wordList; } else if (part.length() == 4) { List<String> wordList = new ArrayList<>(); wordList.add(charToWord(part.charAt(0))); wordList.add("Billion"); char c1 = part.charAt(1); if (c1 != '0') { wordList.add(charToWord(part.charAt(1))); wordList.add("Hundred"); } wordList.addAll(twoCharToWord(part.charAt(2), part.charAt(3))); return wordList; } else { throw new IllegalArgumentException("Unsupported part: " + part); } }
private List<String> twoCharToWord(char c1, char c2) { if (c1 == '0' && c2 == '0') { return Collections.emptyList(); } if (c1 == '0') { return Collections.singletonList(charToWord(c2)); } else if (c1 == '1') { switch (c2) { case '0': return Collections.singletonList("Ten"); case '1': return Collections.singletonList("Eleven"); case '2': return Collections.singletonList("Twelve"); case '3': return Collections.singletonList("Thirteen"); case '4': return Collections.singletonList("Fourteen"); case '5': return Collections.singletonList("Fifteen"); case '6': return Collections.singletonList("Sixteen"); case '7': return Collections.singletonList("Seventeen"); case '8': return Collections.singletonList("Eighteen"); case '9': return Collections.singletonList("Nineteen"); default: throw new IllegalArgumentException("Unsupported char: " + c2); } } else { List<String> wordList = new ArrayList<>(2); switch (c1) { case '2': wordList.add("Twenty"); break; case '3': wordList.add("Thirty"); break; case '4': wordList.add("Forty"); break; case '5': wordList.add("Fifty"); break; case '6': wordList.add("Sixty"); break; case '7': wordList.add("Seventy"); break; case '8': wordList.add("Eighty"); break; case '9': wordList.add("Ninety"); break; default: throw new IllegalArgumentException("Unsupported char: " + c2); }
if (c2 != '0') { wordList.add(charToWord(c2)); } return wordList; } }
private String charToWord(char c) { switch (c) { case '0': return "Zero"; case '1': return "One"; case '2': return "Two"; case '3': return "Three"; case '4': return "Four"; case '5': return "Five"; case '6': return "Six"; case '7': return "Seven"; case '8': return "Eight"; case '9': return "Nine"; default: throw new IllegalArgumentException("Unsupported char: " + c); } } }
|
References
273. Integer to English Words
面试题 16.08. 整数的英语表示