LeetCode刷题实战273:整数转换英文表示
共 3883字,需浏览 8分钟
·
2021-05-29 07:18
Convert a non-negative integer num to its English words representation.
示例
示例 1:
输入:num = 123
输出:"One Hundred Twenty Three"
示例 2:
输入:num = 12345
输出:"Twelve Thousand Three Hundred Forty Five"
示例 3:
输入:num = 1234567
输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
示例 4:
输入:num = 1234567891
输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
解题
class Solution {
private final String[] THOUSAND = {"", "Thousand", "Million", "Billion"};
private final String[] LESS_THAN_TWENTY = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] HUNDRED = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String numberToWords(int num) {
if(num == 0) return "Zero";
StringBuilder sb = new StringBuilder();
int index = 0;
while(num > 0) {
if(num % 1000 != 0) {
StringBuilder tmp = new StringBuilder();
helper(num % 1000, tmp);
sb.insert(0, tmp.append(THOUSAND[index]).append(" "));
}
index++;
num /= 1000;
}
return sb.toString().trim();
}
private void helper(int num, StringBuilder tmp) {
if(num == 0) return;
if(num < 20) {
tmp.append(LESS_THAN_TWENTY[num]).append(" ");
}else if(num < 100) {
tmp.append(HUNDRED[num / 10]).append(" ");
helper(num % 10, tmp);
}else {
tmp.append(LESS_THAN_TWENTY[num / 100]).append(" Hundred").append(" ");
helper(num % 100, tmp);
}
}
}