赞 | 35 |
VIP | 0 |
好人卡 | 0 |
积分 | 74 |
经验 | 0 |
最后登录 | 2024-11-29 |
在线时间 | 476 小时 |
Lv4.逐梦者
- 梦石
- 0
- 星屑
- 7353
- 在线时间
- 476 小时
- 注册时间
- 2021-12-4
- 帖子
- 516
|
找到一个英文版的:
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 + " ");
sb.push(curr.join(''));
}
}
return sb.join('').trim();
}
链接:https://leetcode.cn/problems/int ... en-biao-shi-b-ivik/
可以仿照它做个中文版的? |
|