class Solution {
public:
string numberToWords(int num) {
}
};
273. 整数转换英文表示
将非负整数 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
相似题目
原站题解
java 解法, 执行用时: 1 ms, 内存消耗: 39.8 MB, 提交时间: 2023-04-22 12:00:44
class Solution { String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] thousands = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) { return "Zero"; } StringBuffer sb = new StringBuffer(); for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) { int curNum = num / unit; if (curNum != 0) { num -= curNum * unit; sb.append(toEnglish(curNum)).append(thousands[i]).append(" "); } } return sb.toString().trim(); } public String toEnglish(int num) { StringBuffer curr = new StringBuffer(); int hundred = num / 100; num %= 100; if (hundred != 0) { curr.append(singles[hundred]).append(" Hundred "); } int ten = num / 10; if (ten >= 2) { curr.append(tens[ten]).append(" "); num %= 10; } if (num > 0 && num < 10) { curr.append(singles[num]).append(" "); } else if (num >= 10) { curr.append(teens[num - 10]).append(" "); } return curr.toString(); } }
javascript 解法, 执行用时: 68 ms, 内存消耗: 42.8 MB, 提交时间: 2023-04-22 12:00:11
/** * @param {number} num * @return {string} */ var numberToWords = function(num) { const singles = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]; const teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]; const tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]; const thousands = ["", "Thousand", "Million", "Billion"]; const toEnglish = (num) => { const curr = []; const hundred = Math.floor(num / 100); num %= 100; if (hundred !== 0) { curr.push(singles[hundred] + " Hundred "); } const ten = Math.floor(num / 10); if (ten >= 2) { curr.push(tens[ten] + " "); num %= 10; } if (num > 0 && num < 10) { curr.push(singles[num] + " "); } else if (num >= 10) { curr.push(teens[num - 10] + " "); } return curr.join(''); } if (num === 0) { return "Zero"; } const sb = []; for (let i = 3, unit = 1000000000; i >= 0; i--, unit = Math.floor(unit / 1000)) { const curNum = Math.floor(num / unit); if (curNum !== 0) { num -= curNum * unit; sb.push(toEnglish(curNum) + thousands[i] + " "); } } return sb.join('').trim(); }
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2023-04-22 11:59:54
var ( singles = []string{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"} teens = []string{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"} tens = []string{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"} thousands = []string{"", "Thousand", "Million", "Billion"} ) func numberToWords(num int) string { if num == 0 { return "Zero" } sb := &strings.Builder{} toEnglish := func(num int) { if num >= 100 { sb.WriteString(singles[num/100]) sb.WriteString(" Hundred ") num %= 100 } if num >= 20 { sb.WriteString(tens[num/10]) sb.WriteByte(' ') num %= 10 } if 0 < num && num < 10 { sb.WriteString(singles[num]) sb.WriteByte(' ') } else if num >= 10 { sb.WriteString(teens[num-10]) sb.WriteByte(' ') } } for i, unit := 3, int(1e9); i >= 0; i-- { if curNum := num / unit; curNum > 0 { num -= curNum * unit toEnglish(curNum) sb.WriteString(thousands[i]) sb.WriteByte(' ') } unit /= 1000 } return strings.TrimSpace(sb.String()) }
python3 解法, 执行用时: 36 ms, 内存消耗: 14.8 MB, 提交时间: 2023-04-22 11:59:30
singles = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] thousands = ["", "Thousand", "Million", "Billion"] class Solution: def numberToWords(self, num: int) -> str: if num == 0: return "Zero" def toEnglish(num: int) -> str: s = "" if num >= 100: s += singles[num // 100] + " Hundred " num %= 100 if num >= 20: s += tens[num // 10] + " " num %= 10 if 0 < num < 10: s += singles[num] + " " elif num >= 10: s += teens[num - 10] + " " return s s = "" unit = int(1e9) for i in range(3, -1, -1): curNum = num // unit if curNum: num -= curNum * unit s += toEnglish(curNum) + thousands[i] + " " unit //= 1000 return s.strip()
python3 解法, 执行用时: 40 ms, 内存消耗: 15 MB, 提交时间: 2023-04-22 11:58:38
singles = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] thousands = ["", "Thousand", "Million", "Billion"] class Solution: def numberToWords(self, num: int) -> str: if num == 0: return "Zero" def recursion(num: int) -> str: s = "" if num == 0: return s elif num < 10: s += singles[num] + " " elif num < 20: s += teens[num - 10] + " " elif num < 100: s += tens[num // 10] + " " + recursion(num % 10) else: s += singles[num // 100] + " Hundred " + recursion(num % 100) return s s = "" unit = int(1e9) for i in range(3, -1, -1): curNum = num // unit if curNum: num -= curNum * unit s += recursion(curNum) + thousands[i] + " " unit //= 1000 return s.strip()
golang 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2023-04-22 11:58:13
var ( singles = []string{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"} teens = []string{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"} tens = []string{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"} thousands = []string{"", "Thousand", "Million", "Billion"} ) func numberToWords(num int) string { if num == 0 { return "Zero" } sb := &strings.Builder{} var recursion func(int) recursion = func(num int) { switch { case num == 0: case num < 10: sb.WriteString(singles[num]) sb.WriteByte(' ') case num < 20: sb.WriteString(teens[num-10]) sb.WriteByte(' ') case num < 100: sb.WriteString(tens[num/10]) sb.WriteByte(' ') recursion(num % 10) default: sb.WriteString(singles[num/100]) sb.WriteString(" Hundred ") recursion(num % 100) } } for i, unit := 3, int(1e9); i >= 0; i-- { if curNum := num / unit; curNum > 0 { num -= curNum * unit recursion(curNum) sb.WriteString(thousands[i]) sb.WriteByte(' ') } unit /= 1000 } return strings.TrimSpace(sb.String()) }
javascript 解法, 执行用时: 64 ms, 内存消耗: 42.9 MB, 提交时间: 2023-04-22 11:57:38
/** * @param {number} num * @return {string} */ var numberToWords = function(num) { const singles = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]; const teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]; const tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]; const thousands = ["", "Thousand", "Million", "Billion"]; const recursion = (curr, num) => { if (num === 0) { return; } else if (num < 10) { curr.push(singles[num] + " "); } else if (num < 20) { curr.push(teens[num - 10] + " "); } else if (num < 100) { curr.push(tens[Math.floor(num / 10)] + " "); recursion(curr, num % 10); } else { curr.push(singles[Math.floor(num / 100)] + " Hundred "); recursion(curr, num % 100); } } if (num === 0) { return "Zero"; } const sb = []; for (let i = 3, unit = 1000000000; i >= 0; i--, unit = Math.floor(unit / 1000)) { const curNum = Math.floor(num / unit); if (curNum !== 0) { num -= curNum * unit; const curr = []; recursion(curr, curNum); curr.push(thousands[i] + " "); sb.push(curr.join('')); } } return sb.join('').trim(); }
java 解法, 执行用时: 2 ms, 内存消耗: 39.6 MB, 提交时间: 2023-04-22 11:57:23
class Solution { String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] thousands = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) { return "Zero"; } StringBuffer sb = new StringBuffer(); for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) { int curNum = num / unit; if (curNum != 0) { num -= curNum * unit; StringBuffer curr = new StringBuffer(); recursion(curr, curNum); curr.append(thousands[i]).append(" "); sb.append(curr); } } return sb.toString().trim(); } public void recursion(StringBuffer curr, int num) { if (num == 0) { return; } else if (num < 10) { curr.append(singles[num]).append(" "); } else if (num < 20) { curr.append(teens[num - 10]).append(" "); } else if (num < 100) { curr.append(tens[num / 10]).append(" "); recursion(curr, num % 10); } else { curr.append(singles[num / 100]).append(" Hundred "); recursion(curr, num % 100); } } }