力扣 273. 整数转换英文表示

该博客介绍了一个编程问题,即如何将非负整数转换为对应的英文表示。例如,123应转换为“OneHundredTwentyThree”,12345应转换为“TwelveThousandThreeHundredFortyFive”。提供的Python和Java解决方案分别通过拆分数字并匹配相应的英文词汇来实现这一转换。
摘要由CSDN通过智能技术生成
题目

将非负整数 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();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值