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"
一刷
题解:这题的特殊在于自己的子函数拆分问题。并且注意为整数时不需要再加数字,空格不需要在单位后再添加。
public class Solution {
private final String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private final String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] belowHundred = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String numberToWords(int num) {
if(num == 0) return "Zero";
return helper(num).trim();
}
private String helper(int num){
StringBuilder result = new StringBuilder();
if(num<10) return belowTen[num];
if(num<20) return belowTwenty[num-10];
if(num<100) {
if(num%10 == 0) return belowHundred[num/10];
return result.append(belowHundred[num/10]).append(" ").append(belowTen[num%10]).toString();
}
else if(num<1000) {
if(num%100 == 0) return result.append(helper(num/100)).append(" Hundred").toString();
return result.append(helper(num/100)).append(" Hundred ").append(helper(num%100)).toString();
}
else if(num<1000000) {
if(num%1000 == 0) return result.append(helper(num/1000)).append(" Thousand").toString();
return result.append(helper(num/1000)).append(" Thousand ").append(helper(num%1000)).toString();
}
else if(num<1000000000){
if(num%1000000 == 0) return result.append(helper(num/1000000)).append(" Million").toString();
return result.append(helper(num/1000000)).append(" Million ").append(helper(num%1000000)).toString();
}
else{
if(num%1000000000 == 0) return result.append(helper(num/1000000000)).append(" Billion").toString();
return result.append(helper(num/1000000000)).append(" Billion ").append(helper(num%1000000000)).toString();
}
}
}
二刷
同上
class Solution {
private final String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private final String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] belowHundred = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String numberToWords(int num) {
if(num == 0) return "Zero";
return helper(num).trim();
}
private String helper(int num){
if(num<10) return belowTen[num];
if(num<20) return belowTwenty[num-10];
if(num<100){
if(num%10 == 0) return belowHundred[num/10];
return belowHundred[num/10] + " " + belowTen[num%10];
}
if(num<1000){
if(num%100 == 0) return belowTen[num/100] + " Hundred";
return belowTen[num/100] + " Hundred " + helper(num%100);
}
if(num<1000000){
if(num%1000 == 0) return helper(num/1000) + " Thousand";
return helper(num/1000) + " Thousand " + helper(num%1000);
}
if(num<1000000000){
if(num%1000000 == 0) return helper(num/1000000) + " Million";
return helper(num/1000000) + " Million " + helper(num%1000000);
}
else{
if(num%1000000000 == 0) return helper(num/1000000000) + " Billion";
return helper(num/1000000000) + " Billion " + helper(num%1000000000);
}
}
}