Hard-题目49:273. Integer to English Words

原创 2016年05月31日 23:54:50

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

For example,
123 -> “One Hundred Twenty Three”
12345 -> “Twelve Thousand Three Hundred Forty Five”
1234567 -> “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”
题目大意:
给出一个int类型的数字,输出英文读法。
题目分析:
难得一道原创代码。本题可以说毫无难度(我觉得应该放在easy里面,假期的时候是medium,不知道何时改成hard了)。大模拟而已,就是奇葩情况太多了(毕竟读数字乃是人脑思维),wa时对着test case一点点调试就好了。
源码:(language:java)

public class Solution {
    public String[][] words = {
            {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}, // 0 stand for 0-9
            {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}, //1 stands for 11-19
            {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"} //2 stands for 10-90,please remember 'ninty' is WRONG.
    };
    public String numberToWords(int num) {
        if(num == 0)
            return "Zero";
        String result = "";
        if(num > 0 && num < 1000) {
            result=numberToWords_1_1000(num);
        }
        else if(num >= 1000 && num < 1000000) {
            result = numberToWords_1_1000(num/1000)+" Thousand";
            if(num%1000!=0)
                result = result + " "+numberToWords_1_1000(num%1000);
        }
        else if(num >= 1000000 && num < 1000000000) {
            result = numberToWords_1_1000(num/1000000)+" Million";
            if((num%1000000)/1000!=0) 
                result=result+" "+numberToWords_1_1000((num%1000000)/1000)+" Thousand";
            if(num%1000!=0)
                result=result+" "+numberToWords_1_1000(num%1000);

        }
        else {
            result = numberToWords_1_1000(num/1000000000)+" Billion";
            if((num%1000000000)/1000000!=0) 
                result=result+" "+numberToWords_1_1000((num%1000000000)/1000000) + " Million";
            if((num%1000000)/1000!=0) 
                result=result+" "+ numberToWords_1_1000((num%1000000)/1000)+" Thousand";
            if(num%1000!=0)
                result=result+" "+numberToWords(num%1000);      
        }
        return result;
    }
    private String numberToWords_1_100(int num) {
        if(num<10) // num=0 is a special case,return "" in order to recrusively call it. 
            return words[0][num];
        else if(num == 10)
            return words[2][0]; //return "Ten"
        else if(num < 20) // 11~19
            return words[1][num-11];
        else if(num % 10 == 0) // 20,30,...90
            return words[2][num/10-1];
        else
            return words[2][num/10-1]+" "+words[0][num%10];
    }
    private String numberToWords_1_1000(int num) {
        if(num<100)
            return numberToWords_1_100(num);
        else if(num==100)
            return "One Hundred";
        else if(num%100==0)
            return words[0][num/100]+" Hundred";
        else if(num>100 && num<200)
            return "One Hundred "+numberToWords_1_100(num%100);
        else
            return words[0][num/100]+" Hundred "+numberToWords_1_100(num%100);      
    }
}

成绩:
6ms,12.89%,5ms,44.43%
cmershen的碎碎念:
其实这道题有问题,小学英语老师就教过,百和十之间要有and,同理10^5和10^4,10^8和10^7之间也有,类似于1e6+5这样的数,规范读法是one million and five,在本题中and也省略了。另外测试用例中也没有负数,因此上述代码能够ac(但观察标程的输出可知,标程考虑了负数)
另外再次为大家补习一下小学甚至幼儿园的英语单词:19是nineteen,90是ninety!!!!!!!
ninteen和ninty根本不存在!也不知道谁教出来的这两个词,尤其是ninty。
不知道有多少高中生、大学生甚至研究生还犯这种低级错误……

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

[Leetcode] 273. Integer to English Words

题目: Convert a non-negative integer to its english words representation. Given input is guaranteed ...

[leetcode 273]Integer to English Words

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

LeetCode273:Integer to English Words

Problem Convert a non-negative integer to its english words representation. Given input is guar...

leetcode 273. Integer to English Words

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

【LeetCode】273. Integer to English Words

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

[leetcode] 273. Integer to English Words

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

273. Integer to English Words

题意: Convert a non-negative integer to its english words representation. Given input is guaranteed to...

leetcode笔记:Integer to English Words

题目的要求很简单,将一个整数翻译为英文说法。根据提示我们知道,数字的读法是有规律的,只需每3位作一次翻译处理即可。在循环之前,把可能用到的字符串(0-9、10-19、20、30、40、...、90等数...

【LeetCode】Integer to English Words

其实这题并不难,初中英语学好的童鞋们,都知道trick是什么。但是,细节处比较多,比如说,我后来就focus在空格的增删上。 【我的思路是】 1. 建好hash表。 hash1 : 1~19,2...

Integer to English Words

Integer to English Words
  • J1n5555
  • J1n5555
  • 2015年09月08日 10:32
  • 330
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目49:273. Integer to English Words
举报原因:
原因补充:

(最多只允许输入30个字)