zoukankan      html  css  js  c++  java
  • LeetCode-273 Integer to English Words

    题目描述

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

    题目大意

    要求将一个正整数转化为用英文表示数字的字符串格式。

    示例

    E1

    Input: 123
    Output: "One Hundred Twenty Three"

    E2

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

    E3

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

    E4

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

    解题思路

    麻烦的一道题,麻烦的点在于。。。(看代码就知道了)。

    思想很简单,将数字按照每三位数进行划分,观察示例可知,三位数的表示方式较为固定,可以利用一个函数专门将三位数转化为英文表达式,同时在外循环中,按照规则加入“thousand”,“million”,“billion”等字母。

    应注意,有些特殊数字应给予重视,例如0,1000,100010等,需要进行特殊判断。

    复杂度分析

    时间复杂度:O(N)

    空间复杂度:O(1)

    代码

    class Solution {
    public:
        string numberToWords(int num) {
            if(num == 0)
                return "Zero";
            // Emmmmm......
            tran[1] = "One"; tran[2] = "Two"; tran[3] = "Three"; tran[4] = "Four"; tran[5] = "Five"; tran[6] = "Six"; tran[7] = "Seven"; tran[8] = "Eight"; tran[9] = "Nine"; tran[10] = "Ten"; tran[11] = "Eleven"; tran[12] = "Twelve"; tran[13] = "Thirteen"; tran[14] = "Fourteen"; tran[15] = "Fifteen"; tran[16] = "Sixteen"; tran[17] = "Seventeen"; tran[18] = "Eighteen"; tran[19] = "Nineteen"; tran[20] = "Twenty"; tran[30] = "Thirty"; tran[40] = "Forty"; tran[50] = "Fifty"; tran[60] = "Sixty"; tran[70] = "Seventy"; tran[80] = "Eighty"; tran[90] = "Ninety";
            // 需要单独保存这三个单词
            div[0] = "Thousand"; div[1] = "Million"; div[2] = "Billion";
            
            string res = "";
            int tmp = 0, k = 0;
            // 将整数依次缩小置零,每次只判断三位数
            while(num) {
                // 取后三位数
                tmp = num % 1000;
                num /= 1000;
                // 如果后三位不为零,则将其转换的字符串加入结果
                if(tmp != 0) {
                    res.insert(0, its(tmp));
                }
                // 如果之后的num的后三位不为零,则加入三个数量级其中之一
                if(num % 1000) {
                    res.insert(0, " " + div[k] + (res.length() == 0 ? "" : " "));
                }
                ++k;
            }
            
            return res;
        }
        // 将三位的整数转化为字符串
        string its(int num) {
            string res = "";
            // 如果num为三位数
            if(num >= 100) {
                res += (tran[num / 100] + " Hundred");
                if(num % 100 != 0)
                    res += (" " + its(num % 100));
            }
            // 如果num为两位数
            else if(num >= 10){
                // 如果数字为10-19,则需特殊赋值
                if(num < 20) {
                    res += tran[num];
                    return res;
                }
                // 否则正常计算
                else
                    res += tran[num - num % 10];
                if(num % 10 != 0)
                    res += (" " + its(num % 10));
            }
            // 如果num为个位数
            else if(num > 0){
                res += tran[num];
            }
            
            return res;
        }
        
    private:
        map<int, string> tran;
        map<int, string> div;
    };
  • 相关阅读:
    Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......
    UVA 1597 Searching the Web
    UVA 1596 Bug Hunt
    UVA 230 Borrowers
    UVA 221 Urban Elevations
    UVA 814 The Letter Carrier's Rounds
    UVA 207 PGA Tour Prize Money
    UVA 1592 Database
    UVA 540 Team Queue
    UVA 12096 The SetStack Computer
  • 原文地址:https://www.cnblogs.com/heyn1/p/11127374.html
Copyright © 2011-2022 走看看