力扣273(java)-整数转换英文表示(困难)
题目:
将非负整数 num 转换为其对应的英文表示。
示例 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"
提示:
0 <= num <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/integer-to-english-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
一、递归
将整数转换成英文表示,三个数值为一组,使用递归的方式将每一组用英文表示,并添加上每一组对应的单位词后拼接即可,每一组的处理方式如下:
- 处理特殊数字0,返回空(如果原数为0,直接返回"Zero");
- 小于20的数,直接用对应的英文表示即可;
- [ 20,100) 之间的数将十位转换成英文表示,对个位递归转换成英文表示;
- 大于等于100的数,将百位转换成英文表示,对十位个位进行递归转换成英文表示。
代码:
1 class Solution { 2 String[] oneToTwenty = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ","Ten ", 3 "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ", "Twenty "}; 4 String[] tens = {"", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}; 5 String[] thousands = {"", "Thousand ", "Million ", "Billion "};//"",千,百万,十亿 6 7 public String numberToWords(int num) { 8 //处理0 9 if(num == 0) return "Zero"; 10 //处理小于1000的数 11 if(num < 1000) return thousandsLow(num).trim(); 12 13 StringBuilder sb = new StringBuilder(); 14 int index = 0; 15 while(num > 0){ 16 //每三位截取为一段 17 if(num % 1000 != 0) 18 //插入到开头的位置,第一次取的是最后三位数 19 sb.insert(0,thousandsLow(num % 1000) + thousands[index]); 20 num = num / 1000; 21 index++; 22 } 23 return sb.toString().trim(); 24 } 25 //一千以下的单独处理 26 public String thousandsLow(int num){ 27 if(num == 0) return ""; 28 //小于20直接返回对应的数字 29 if(num < 20) return oneToTwenty[num]; 30 //20-100 31 else if(num < 100) return tens[num / 10] + thousandsLow(num % 10); 32 //100-1000 33 else return oneToTwenty[num / 100] + "Hundred "+ thousandsLow(num % 100); 34 } 35 }
二、迭代
分情况讨论每一段:[1,20]、[21,99]、[100, 999]
- [1,20]直接转为对应的单词;
- [21,99],先取十位,再将个位按[1,20]转为对应的单词;
- [100,999],先取百位再加上单位,剩下的十位和个位按照[1,20]和[21,99]转为对应的单词。
代码:
1 class Solution { 2 public static String[] oneToTwenty = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ","Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ", "Twenty "}; 3 public static String[] tens = {"", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}; 4 public static String[] thousands = {"Billion ", "Million ", "Thousand ", ""};//"",千,百万,十亿 5 public static int[] mod = {1000000000, 1000000, 1000, 1}; 6 7 public String numberToWords(int n) { 8 //处理0 9 if(n == 0) return "Zero"; 10 11 StringBuilder sb = new StringBuilder(); 12 int i = 0; 13 while(n > 0){ 14 //每三位截取为一段 15 int x = n / mod[i]; 16 if(x > 0){ 17 toWordsFor1To999(sb, x); 18 //加上单位 19 sb.append(thousands[i]); 20 } 21 n = n % mod[i]; 22 i++; 23 } 24 return sb.toString().trim(); 25 } 26 //1-20转成单词加入到sb中 27 public static void toWordsFor1To20(StringBuilder sb, int n){ 28 sb.append(oneToTwenty[n]); 29 } 30 //21-99转成单词加入到sb中 31 public static void toWordsFor21To99(StringBuilder sb, int n){ 32 //取十位 33 int num = n / 10; 34 sb.append(tens[num]); 35 //取个位 36 int x = n % 10; 37 if(x > 0) toWordsFor1To20(sb, x); 38 } 39 //100-999转换成单词加入到sb中 40 public static void toWordsFor100To999(StringBuilder sb, int n){ 41 //取百位 42 int x = n / 100; 43 toWordsFor1To20(sb, x); 44 //加上百的单位 45 sb.append("Hundred "); 46 int y = n % 100; 47 if(y > 20) toWordsFor21To99(sb, y); 48 else if(y > 0) toWordsFor1To20(sb, y); 49 } 50 //1-999 51 public static void toWordsFor1To999(StringBuilder sb, int n){ 52 if(n >= 0 && n <= 20){ 53 toWordsFor1To20(sb, n); 54 }else if(n > 20 && n < 100){ 55 toWordsFor21To99(sb, n); 56 }else{ 57 toWordsFor100To999(sb, n); 58 } 59 }
60 }
小知识:
trim() 方法用于删除字符串的头尾空白符