zoukankan      html  css  js  c++  java
  • [LeetCode]47. Integer to English Words整数的读法

    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"

    Hint:

    1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
    2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
    3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

    解法:按billion、million、thousand和hundred及以下依次处理即可。

    class Solution {
    public:
        string numberToWords(int num) {
            vector<string> unit = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
            vector<string> dec1 = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            vector<string> dec2 = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
            vector<string> expr = { "Thousand", "Million", "Billion" };
            string res = "";
            for (int i = 9; i >= 0; i -= 3)
            {
                int div = num / (int)pow(10, i);
                if (div > 0 && div < 10)
                {
                    res += unit[div - 1] + " ";
                    if (i > 0) res += expr[i / 3 - 1] + " ";
                }
                else if (div >= 10 && div < 20)
                {
                    res += dec1[div - 10] + " ";
                    if (i > 0) res += expr[i / 3 - 1] + " ";
                }
                else if (div >= 20)
                {
                    if (div >= 20 && div < 100)
                    {
                        int bas = div / 10, reu = div % 10;
                        res += dec2[bas] + " ";
                        if (reu > 0) res += unit[reu - 1] + " ";
                        if (i > 0) res += expr[i / 3 - 1] + " ";                        
                    }
                    else if (div >= 100)
                    {
                        int hun = div / 100, tne = div / 10 % 10, uni = div % 10;
                        res += unit[hun - 1] + " Hundred ";
                        if (tne == 1) res += dec1[div % 100 - 10] + " ";
                        else if (tne > 1 || uni > 0)
                        {
                            if(tne > 1) res += dec2[tne] + " ";
                            if (uni > 0) res += unit[uni - 1] + " ";
                        }
                        if (i > 0) res += expr[i / 3 - 1] + " ";
                    }
                }
                num %= (int)pow(10, i);
                if (num <= 0) break;
            }
            if (res.empty()) res += "Zero ";
            res.resize(res.size() - 1);
            return res;
        }
    };

    改进:将输入整数用billion、million、thousand除后,所得的商均在0-999之间,因此可以将0-999之间的读法单独提出来,作为一个工具函数。

    class Solution {
    public:
        string numberToWords(int num) {
            if (num == 0) return "Zero";
            vector<string> expr = { "Thousand", "Million", "Billion" };
            string res = "";
            for (int i = 9; i >= 0; i -= 3)
            {
                int div = num / (int)pow(10, i);
                if (div > 0)
                {
                    res += lessThanThousand(div);
                    if (i > 0) res += expr[i / 3 - 1] + " ";
                }
                num %= (int)pow(10, i);
            }
            res.resize(res.size() - 1);
            return res;
        }
    private:
        string lessThanThousand(int num)
        {
            vector<string> unit = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
            vector<string> decd = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
            string res = "";
            int hun = num / 100, ten = num / 10 % 10, uni = num % 10;
            if (hun > 0) res += unit[hun - 1] + " Hundred ";
            if (ten == 1) res += decd[ten * 10 + uni - 10] + " ";
            else if (ten > 1 || uni > 0)
            {
                if (ten > 1) res += decd[8 + ten] + " ";
                if (uni > 0) res += unit[uni - 1] + " ";
            }
            return res;
        }
    };
  • 相关阅读:
    推荐一款高端大气上档次的在线作图工具
    13个优秀的开源UML工具介绍
    本节向大家介绍一下UML建模误区
    推荐一款好用轻便的在线UML画图工具
    恭喜你,Get到一份 正则表达式 食用指南
    SpringBoot图文教程7—SpringBoot拦截器的使用姿势这都有
    SpringBoot图文教程6—SpringBoot中过滤器的使用
    SpringBoot图文教程5—SpringBoot 中使用Aop
    什么?接口中方法可以不是抽象的「JDK8接口新语法的深度思考」
    SpringBoot图文教程4—SpringBoot 实现文件上传下载
  • 原文地址:https://www.cnblogs.com/aprilcheny/p/4919849.html
Copyright © 2011-2022 走看看