Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 273. Integer to English Words #273

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 273. Integer to English Words #273

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

 

Convert a non-negative integer num to its English words representation.

 

Example 1:

Input: num = 123
Output: "One Hundred Twenty Three"

Example 2:

Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"

Example 3:

Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Example 4:

Input: num = 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

 

Constraints:

  • 0 <= num <= 231 - 1

 

这道题让我们把一个整型数转为用英文单词描述,就像在 check 上写钱数的方法,博主最开始的方法特别复杂,用了几个 switch 语句来列出所有的单词,但是看网上大神们的解法都是用数组来枚举的,特别的巧妙而且省地方,膜拜学习中。题目中给足了提示,首先说是要3个一组的进行处理,而且题目中限定了输入数字范围为0到 231 - 1 之间,最高只能到 billion 位,3个一组也只需处理四组即可,那么需要些一个处理三个一组数字的函数,需要把1到 19 的英文单词都列出来,放到一个数组里,还要把 20,30,... 到 90 的英文单词列出来放到另一个数组里,然后还需要用些技巧,比如一个三位数n,百位数表示为 n/100,后两位数一起表示为 n%100,十位数表示为 n%100/10,个位数表示为 n%10,然后看后两位数是否小于 20,小于的话直接从数组中取出单词,如果大于等于 20 的话,则分别将十位和个位数字的单词从两个数组中取出来。然后再来处理百位上的数字,还要记得加上 Hundred。主函数中调用四次这个帮助函数,然后中间要插入 "Thousand", "Million", "Billion" 到对应的位置,最后 check 一下末尾是否有空格,把空格都删掉,返回的时候检查下输入是否为0,是的话要返回 'Zero',参见代码如下:

 

class Solution {
public:
    string numberToWords(int num) {
        string res = convertHundred(num % 1000);
        vector<string> v = {"Thousand", "Million", "Billion"};
        for (int i = 0; i < 3; ++i) {
            num /= 1000;
            res = num % 1000 ? convertHundred(num % 1000) + " " + v[i] + " " + res : res;
        }
        while (res.back() == ' ') res.pop_back();
        return res.empty() ? "Zero" : res;
    }
    string convertHundred(int num) {
        vector<string> v1 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
        vector<string> v2 = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        string res;
        int a = num / 100, b = num % 100, c = num % 10;
        res = b < 20 ? v1[b] : v2[b / 10] + (c ? " " + v1[c] : "");
        if (a > 0) res = v1[a] + " Hundred" + (b ? " " + res : "");
        return res;
    }
};

 

Github 同步地址:

#273

 

类似题目:

Integer to Roman

 

参考资料:

https://leetcode.com/problems/integer-to-english-words/

https://leetcode.com/problems/integer-to-english-words/discuss/70627/Short-clean-Java-solution

https://leetcode.com/problems/integer-to-english-words/discuss/70625/My-clean-Java-solution-very-easy-to-understand

 

LeetCode All in One 题目讲解汇总(持续更新中...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant