题目
将非负整数 num 转换为其对应的英文表示。
示例
输入:num = 123
输出:“One Hundred Twenty Three”
输入:num = 12345
输出:“Twelve Thousand Three Hundred Forty Five”
输入:num = 1234567
输出:“One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”
输入:num = 1234567891
输出:“One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One”
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/integer-to-english-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
想法
好尼玛抽象的题。。。好累
实现
代码1:python
class Solution:
def numberToWords(self, num: int) -> str:
var, vs = 0, 0
st, res, out = list(), list(), str()
if num == 0:
return ("Zero")
dy = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten',
'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen',
'Eighteen', 'Nineteen', 'Twenty', 'Twenty One', 'Twenty Two', 'Twenty Three',
'Twenty Four', 'Twenty Five', 'Twenty Six', 'Twenty Seven', 'Twenty Eight',
'Twenty Nine', 'Thirty', 'Thirty One', 'Thirty Two', 'Thirty Three',
'Thirty Four', 'Thirty Five', 'Thirty Six', 'Thirty Seven', 'Thirty Eight',
'Thirty Nine', 'Forty', 'Forty One', 'Forty Two', 'Forty Three', 'Forty Four',
'Forty Five', 'Forty Six', 'Forty Seven', 'Forty Eight', 'Forty Nine', 'Fifty',
'Fifty One', 'Fifty Two', 'Fifty Three', 'Fifty Four', 'Fifty Five',
'Fifty Six', 'Fifty Seven', 'Fifty Eight', 'Fifty Nine', 'Sixty',
'Sixty One', 'Sixty Two', 'Sixty Three', 'Sixty Four', 'Sixty Five',
'Sixty Six', 'Sixty Seven', 'Sixty Eight', 'Sixty Nine', 'Seventy',
'Seventy One', 'Seventy Two', 'Seventy Three', 'Seventy Four',
'Seventy Five', 'Seventy Six', 'Seventy Seven', 'Seventy Eight',
'Seventy Nine', 'Eighty', 'Eighty One', 'Eighty Two', 'Eighty Three',
'Eighty Four', 'Eighty Five', 'Eighty Six', 'Eighty Seven', 'Eighty Eight',
'Eighty Nine', 'Ninety', 'Ninety One', 'Ninety Two', 'Ninety Three', 'Ninety Four',
'Ninety Five', 'Ninety Six', 'Ninety Seven', 'Ninety Eight', 'Ninety Nine']
dw = ['', 'Thousand', 'Million', 'Billion']
while (num):
var = num % 1000 # 3个一组
num = int(num / 1000)
var = str(var)
if len(var) == 3:
for i in range(len(var)):
vs = int(var[i]) * (10 ** (len(var) - 1 - i))
if vs >= 100:
st.append(dy[int(str(vs)[0])])
st.append('Hundred')
elif vs!=0 and vs<11 and i!=2:
vs=int(var[2])+int(var[1])*10
st.append(dy[vs])
break
elif vs!=0:
st.append(dy[vs])
elif int(var)==0:
st.append('0')
else:
vs=int(var)
st.append(dy[vs])
res.append(st)
st = []
for i in range(len(res) - 1, -1, -1):
if res[i][0] !='0':
out += " ".join(res[i])
out += " "
out += str(dw[i])
out += " "
return (out.strip())
方法2:java
class Solution {
public String numberToWords(int num) {
if (num == 0) {
return "Zero";
}
// 整型转字符串
String numStr = String.valueOf(num);
// 获取数组长度
int len = 0;
if (numStr.length() % 3 == 0) {
len = numStr.length() / 3;
} else {
len = numStr.length() / 3 + 1;
}
// 用数组每隔三位数字存储
String[] nums = new String[len];
if (numStr.length() % 3 == 0) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
nums[i] = numStr.substring(count, count + 3);
count += 3;
}
} else {
int count = numStr.length() % 3;
nums[0] = numStr.substring(0, count);
for (int i = 1; i < nums.length; i++) {
nums[i] = numStr.substring(count, count + 3);
count += 3;
}
}
// 每三位数字单位
String[] units = {"", "Thousand ", "Million ", "Billion "};
StringBuilder res = new StringBuilder();
for (String str : nums) {
if (str.equals("000")) {
len--;
continue;
}
res.append(getEnglishNum(str)).append(units[len - 1]);
len--;
}
return res.toString().trim();
}
// 三位数及以下数字转为英文
public String getEnglishNum(String numStr) {
String[] hundreds = {"", "One Hundred ", "Two Hundred ", "Three Hundred ", "Four Hundred ", "Five Hundred ", "Six Hundred ", "Seven Hundred ", "Eight Hundred ", "Nine Hundred "};
String[] tens = {"", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
String[] tenOnes = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
String[] ones = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
int num = Integer.parseInt(numStr);
StringBuilder sb = new StringBuilder();
// 百位
sb.append(hundreds[num / 100]);
// 十位和个位
if ((num % 100) / 10 == 1) {
sb.append(tenOnes[num % 10]);
} else {
sb.append(tens[(num % 100) / 10]).append(ones[num % 10]);
}
// 个位
return sb.toString();
}
}