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];
    }
}

 

posted on 2022-04-12 07:04  阳光明媚的菲越  阅读(16)  评论(0编辑  收藏  举报