原题网址:https://leetcode.com/problems/integer-to-english-words/
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)
public class Solution {
private String[] tens = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private String[] teens = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private String[] ones = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private String[] thousands = {"Thousand", "Million", "Billion", "Trillion"};
private StringBuilder sb = new StringBuilder();
private void insert(String word) {
if (sb.length() > 0) sb.insert(0, " ");
sb.insert(0, word);
}
private void number(int num, int thousand) {
if (num <= 0) return;
int m1000 = num % 1000;
if (m1000 > 0 && thousand > 0) insert(thousands[thousand-1]);
int m100 = num % 100;
int m10 = num % 10;
if (m100 > 0) {
if (m10 == 0) {
insert(tens[m100/10-1]);
} else if (11 <= m100 && m100 <= 19) {
insert(teens[m100-11]);
} else if (m100 < 11) {
insert(ones[m100-1]);
} else {
insert(ones[m10-1]);
insert(tens[m100/10-1]);
}
}
int h = num % 1000 / 100;
if (h > 0) {
insert("Hundred");
insert(ones[h-1]);
}
number(num/1000, thousand+1);
}
public String numberToWords(int num) {
if (num == 0) return "Zero";
number(num, 0);
return sb.toString();
}
}
另一种实现方式:
public class Solution {
private String[] thousands = {"", "Thousand", "Million", "Billion"};
private String[] ones = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private String[] teens = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private String[] tens = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private String hundred(int num) {
String words = "";
int ten = num % 100;
if (ten > 0 && ten % 10 == 0) words = tens[ten / 10 - 1];
else if (11 <= ten && ten <= 19) words = teens[ten-11];
else if (0 < ten && ten < 10) words = ones[ten];
else if (ten >= 20) words = tens[ten / 10 - 1] + " " + ones[ten % 10];
num /= 100;
if (num > 0) words = ones[num] + " Hundred" + (words.length()==0? "" : " ") + words;
return words;
}
public String numberToWords(int num) {
if (num == 0) return "Zero";
String words = "";
int t = 0;
while (num > 0) {
int hundred = num % 1000;
if (hundred > 0) words = hundred(hundred) + (thousands[t].length()==0? "": " ") + thousands[t] + (words.length()==0? "": " ") + words;
num /= 1000;
t ++;
}
return words;
}
}

本文介绍了一种将非负整数转换为其英文单词表示的方法。通过递归加分治策略,利用一千为单位进行分组,实现了从1到2^31 - 1范围内的整数转换。
1491

被折叠的 条评论
为什么被折叠?



