[LeetCode] 273. Integer to English Words

本文介绍了一种将非负整数转换为英文单词表示的方法,适用于小于2^31-1的整数。通过将数字分解为每三位一组,并分别处理百位数和其他位数,实现了从个位到十亿级别的数字转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接:https://leetcode.com/problems/integer-to-english-words/

1. Description

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

Example 1:

Input: 123
Output: "One Hundred Twenty Three"

Example 2:

Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"

Example 3:

Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Example 4:

Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

2. Solution

My Solution is as same as the LeetCode official Solution: https://leetcode.com/problems/integer-to-english-words/solution/

We need to split the number by 3-digits parts. Every 3-digits parts split by two parts: the digit of hundred and the rest.
The numbers between 11 and 20 need to be considered separately.

It is an easy but quite sticky problem. Be careful, good luck!
code

class Solution {
    public String numberToWords(int num) {
        //First,check if num is zero
        if(num == 0){
            return "Zero";
        }
        //simplify the problem:
        //split the Interger , every parts has 3 digits
        //subproblem: convert 3 digits integer to English words.
        
        int billion = num / 1000000000;
        int million = (num - (billion * 1000000000) ) / 1000000;
        int thousand= (num - (billion * 1000000000) -(million * 1000000) ) / 1000;
        int ones = num - (billion * 1000000000) - (million * 1000000) - (thousand * 1000);

        String ans = "";
        if(billion != 0){
            ans += threedigits(billion) + " Billion";
        }
        if(million != 0){
            if( !ans.isEmpty() ){
                ans += " ";
            }
            ans += threedigits(million) + " Million";
        }
        if(thousand != 0){
            if( !ans.isEmpty() ){
                ans += " ";
            }
            ans += threedigits(thousand) + " Thousand";
        }
        if(ones != 0){
            if( !ans.isEmpty() ){
                ans += " ";
            }
            ans += threedigits(ones);
        }
        return ans;
    }
    
    // n>100 -> oneDigit(n/100) , twoDigit(n%100)
    // n%100 == 0 -> oneDigit(n/100)
    // n<100 -> twoDigit(n%100)
    public String threedigits(int n){
        int hundred = n/100;
        int rest = n%100;
        String s = "";
        if(hundred != 0 && rest != 0){
            return s + oneDigit(hundred)+" Hundred "+twoDigit(rest);
        }else if(hundred != 0 && rest ==0){
            return s + oneDigit(hundred)+" Hundred";
        }else{
            return twoDigit(rest);
        }
    }
    
    //11-19 -> betweenTenTwenty(n)
    //21-99 -> tenDigit(n%10) , oneDigit(n/10)
    //the digits of one is 0 -> ten(n)
    public String twoDigit(int n){
        int ten = n/10;
        int one = n%10;
        if(ten == 0){
            return oneDigit(one);
        }else{
            if(one == 0){
                return ten(ten);
            }else{
                return ten == 1 ? betweenTenTwenty(n) : ( ten(ten) + " " + oneDigit(one));
            }
        }
    }
    
    //n less than ten
    //from 1 to 9
    public String oneDigit(int n){
        if(n == 1){
            return "One";
        }else if(n ==2){
            return "Two";
        }else if(n ==3){
            return "Three";
        }else if(n ==4){
            return "Four";
        }else if(n ==5){
            return "Five";
        }else if(n ==6){
            return "Six";
        }else if(n ==7){
            return "Seven";
        }else if(n ==8){
            return "Eight";
        }else if(n ==9){
            return "Nine";
        }else{
            return "";
        }
    }
    
    //n is divisible by 10
    public String ten(int n){
        if(n == 1){
            return "Ten";
        }else if (n ==2){
            return "Twenty";
        }else if (n ==3){
            return "Thirty";
        }else if (n ==4){
            return "Forty";
        }else if (n ==5){
            return "Fifty";
        }else if (n ==6){
            return "Sixty";
        }else if (n ==7){
            return "Seventy";
        }else if (n ==8){
            return "Eighty";
        }else if (n ==9){
            return "Ninety";
        }else{
            return "";
        }
    }
    
    //11-19
    public String betweenTenTwenty(int n){
        if(n == 11){
            return "Eleven";
        }else if(n == 12){
            return "Twelve";
        }else if(n == 13){
            return "Thirteen";
        }else if(n == 14){
            return "Fourteen";
        }else if(n == 15){
            return "Fifteen";
        }else if(n == 16){
            return "Sixteen";
        }else if(n == 17){
            return "Seventeen";
        }else if(n == 18){
            return "Eighteen";
        }else if(n == 19){
            return "Nineteen";
        }else {
            return "";
        }
    }
}

3. Reference

https://leetcode.com/problems/integer-to-english-words/solution/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值