码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 273. Integer to English Words

时间:2019-01-29 23:09:59      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:string   etc   color   english   case   现在   rest   and   number   

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"
 

 

最难的是要想到将一位数/二位数/三位数的方法单独列出,然后再判断是否加上billlion之类的。

很多细节需要考虑。

 

class Solution {
    public String one(int num) {
            switch(num) {
                case 1: return "One";
                case 2: return "Two";
                case 3: return "Three";
                case 4: return "Four";
                case 5: return "Five";
                case 6: return "Six";
                case 7: return "Seven";
                case 8: return "Eight";
                case 9: return "Nine";
                    
            }
            return "";
        }
        
        public String twoSmallerThan20(int num){
            switch(num) {
                case 10: return "Ten";
                case 11: return "Eleven";
                case 12: return "Twelve";
                case 13: return "Thirteen";
                case 14: return "Fourteen";
                case 15: return "Fifteen";
                case 16: return "Sixteen";
                case 17: return "Seventeen";
                case 18: return "Eighteen";
                case 19: return "Nineteen";
            }
            return "";
        }
        
        public String ten(int num) {
            switch(num) {
                case 2: return "Twenty";
                case 3: return "Thirty";
                case 4: return "Forty";
                case 5: return "Fifty";
                case 6: return "Sixty";
                case 7: return "Seventy";
                case 8: return "Eighty";
                case 9: return "Ninety";
            }
            return "";
        }
        
        public String two(int num) {
            if(num == 0) return "";
            if(num < 10) {
                return one(num);
            }else if(num < 20) {
                return twoSmallerThan20(num);
            }else {
                int tenner = num/10;
                int rest = num - 10*tenner;
                if(rest != 0) {
                    return ten(tenner) + " " + one(rest);
                }else{
                    return ten(tenner);
                }
                
            }
        }
    
    public String three(int num) {  //其实前面的几个方法都是为这个方法服务的。只有three()会出现在最后的主函数里。
        int hundred = num/100;
        int rest = num - 100*hundred;
        if(hundred*rest != 0) {
            return one(hundred) + " Hundred " + two(rest); 
        }else if(hundred == 0){
            return two(rest);
        }else{
            return one(hundred) + " Hundred"; //单独列出这个是因为这里的Hundred后面没有空格。
        }
    }
        
    
    public String numberToWords(int num) {
        if(num == 0) return "Zero";
        int billion = num/1000000000;
        int million = (num-billion*1000000000)/1000000;
        int thousand = (num-billion*1000000000-million*1000000)/1000;
        int rest = num - billion*1000000000-million*1000000 - thousand*1000;
        String result = "";
        if(billion != 0){
            result += three(billion) + " Billion";
        }
        if(million != 0){
            if(result != "") result += " "; //这里一定要判断一下要不要加空格
            result += three(million) + " Million";
        }
        if(thousand != 0){
            if(result != "") result += " ";
            result += three(thousand) + " Thousand";
        }
        if(rest != 0){
            if(result != "") result += " ";
            result += three(rest);
        }
        return result;
    }
}

 

leetcode 273. Integer to English Words

标签:string   etc   color   english   case   现在   rest   and   number   

原文地址:https://www.cnblogs.com/jamieliu/p/10336174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!