Leetcode 273 Integer to English Words

HowieLee59發表於2019-01-03

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"

這個題的意思是將數字轉化為英語表達出來,寫的時候要注意單詞拼寫。

程式碼如下:

class Solution {
    private final String[] belowTen = new String[]{"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
    private final String[] belowTwenty = new String[]{"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    private final String[] belowHundred = new String[]{"","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    public String numberToWords(int num) {
        if(num == 0){
            return "Zero";
        }//注意邊界條件
        return helper(num);
    }
    public String helper(int num){
        String result = "";
        if(num < 10){
            result = belowTen[num];
        }else if(num < 20){
            result = belowTwenty[num - 10];
        }else if(num < 100){
            result = belowHundred[num / 10] + " " + belowTen[num % 10];
        }else if(num < 1000){ 
            result = belowTen[num / 100] + " Hundred " + helper(num % 100);
        }else if(num < 1000000){
            result = helper(num / 1000) + " Thousand " + helper(num % 1000);
        }else if (num < 1000000000){
            result = helper(num/1000000) + " Million " +  helper(num % 1000000);
        }else{
            result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
        }
        return result.trim();//去掉前面和後面的空格。
    }
}

 

相關文章