Leetcode 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

cpp 复制代码
class Solution {
public:
    
    string num0_19[20] = {
        "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
        "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
    };
    string num20_90[8] = {
        "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety",
    };
    string num1000[5] = {
        "Billion ", "Million ", "Thousand ", "",
    };

    string get(int x) {
        string res;
        if (x >= 100) {
            res += num0_19[x / 100] + " Hundred ";
            x %= 100;
        }
        if (x >= 20) {
            res += num20_90[x / 10 - 2] + " ";
            x %= 10;
            if (x) res += num0_19[x] + ' ';
        } else if (x) {
            res += num0_19[x] + ' ';
        }
        return res;
    }

    string numberToWords(int num) {
        if(!num) return "Zero";
        string res;
        for(int i = 1e9, j = 0; i >= 1; i /= 1000, j ++ )
            if(num >= i) {
                res += get(num / i) + num1000[j];
                num %= i;
            }
        res.pop_back();
        return res;
    }
};
相关推荐
米粒12 小时前
力扣算法刷题 Day 63 Bellman_ford 算法
数据库·算法·leetcode
木井巳15 小时前
【递归算法】单词搜索
java·算法·leetcode·决策树·深度优先
Chase_______16 小时前
LeetCode 2461 & 1423:定长滑窗变体精讲,从 HashMap 判重到正难则反的转化技巧
算法·leetcode·职场和发展
sheeta199818 小时前
LeetCode 每日一题笔记 日期:2026.05.07 题目:3660. 找到所有可以到达的最大值
笔记·算法·leetcode
Hesionberger18 小时前
LeetCode79:单词搜索DFS回溯详解
java·开发语言·c++·python·算法·leetcode·c#
米粒119 小时前
力扣算法刷题 Day 62 最短路算法
算法·leetcode·职场和发展
小雅痞20 小时前
[Java][Leetcode hard] 30. 串联所有单词的子串
java·leetcode
khalil102020 小时前
代码随想录算法训练营Day-43 动态规划10 | 300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
数据结构·c++·算法·leetcode·动态规划·子序列问题
风筝在晴天搁浅20 小时前
字节/蚂蚁/美团/拼多多 LeetCode 165.比较版本号
java·leetcode
悲伤小伞20 小时前
LeetCode 热题 100_3-128. 最长连续序列
c++·算法·leetcode·哈希算法