273. 整数转换英文表示
将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。
示例 1:
输入: 123
输出: “One Hundred Twenty Three”
示例 2:
输入: 12345
输出: “Twelve Thousand Three Hundred Forty Five”
示例 3:
输入: 1234567
输出: “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”
示例 4:
输入: 1234567891
输出: “One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One”
class Solution {
final static String[] zeroToNineteen = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"};
final static String[] twentyToNinety = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
final int BILLION = 1000000000;
final int MILLION = 1000000;
final int THOUSAND = 1000;
final int HUNDRED = 100;
public String numberToWords(int num) {
if (num == 0) return "Zero";
StringBuilder builder = new StringBuilder();
if (num >= BILLION) {
if (builder.length() != 0) builder.append(" ");
builder.append(numberToWords(num / BILLION)).append(" Billion");
num %= BILLION;
}
if (num >= MILLION) {
if (builder.length() != 0) builder.append(" ");
builder.append(numberToWords(num / MILLION)).append(" Million");
num %= MILLION;
}
if (num >= THOUSAND) {
if (builder.length() != 0) builder.append(" ");
builder.append(numberToWords(num / THOUSAND)).append(" Thousand");
num %= THOUSAND;
}
if (num >= HUNDRED) {
if (builder.length() != 0) builder.append(" ");
builder.append(numberToWords(num / HUNDRED)).append(" Hundred");
num %= HUNDRED;
}
if (num < 20) {
if (num != 0) {
if (builder.length() != 0) builder.append(" ");
builder.append(zeroToNineteen[num]);
}
}
else {
if (builder.length() != 0) builder.append(" ");
builder.append(twentyToNinety[num / 10 - 2]);
if (num % 10 != 0) {
if (builder.length() != 0) builder.append(" ");
builder.append(zeroToNineteen[num % 10]);
}
}
return builder.toString();
}
}