其他分享
首页 > 其他分享> > 273. Integer to English Words

273. Integer to English Words

作者:互联网

class Solution {
    private String[] lessThanTwenty = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
            "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
            "Nineteen" };
    private String[] lessThanHundred = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

    public String numberToWords(int num) {
        if(num<0)
            return "";
        if(num==0)
            return "Zero";
        return helper(num).trim();
    }
    
    private String helper(int num){
        if(num>=1000000000){
            int part1 = num/1000000000;
            int rest = num%1000000000;
            return helper(part1).trim()+" Billion "+helper(rest).trim();
        }
        if(num>=1000000){
            int part1 = num/1000000;
            int rest = num%1000000;
            return helper(part1).trim()+" Million "+helper(rest).trim();
        }
        if(num>=1000){
            int part1 = num/1000;
            int rest = num%1000;
            return helper(part1).trim()+" Thousand "+helper(rest).trim();
        }
        if(num>=100){
            int part1 = num/100;
            int rest = num%100;
            return lessThanTwenty[part1]+" Hundred "+helper(rest).trim();
        }
        if(num>=20){
            int part1 = num/10;
            int rest = num%10;
            return lessThanHundred[part1]+" "+helper(rest).trim();
        }
        return lessThanTwenty[num];
    }
}

 

标签:trim,num,helper,int,rest,part1,273,Words,Integer
来源: https://www.cnblogs.com/feiflytech/p/16133386.html