程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> leetcode筆記:Integer to English Words

leetcode筆記:Integer to English Words

編輯:關於C++

一. 題目描述

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:

Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. 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. 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)

二. 題目分析

題目的要求很簡單,將一個整數翻譯為英文說法。根據提示我們知道,數字的讀法是有規律的,只需每3位作一次翻譯處理即可。在循環之前,把可能用到的字符串(0-9、10-19、20、30、40、…、90等數的英語)進行歸類,放在幾個數組裡,分情況使用。在累加字符串時注意空格的使用。另一個需要注意的是num的最大值為2,147,483,647,因此使用到的最高的單位是"Billion"

三. 示例代碼

class Solution
{
public:
    string numberToWords(int num) {
        //  num的最大值:2,147,483,647
        const string unit[4] = {"", " Thousand", " Million", " Billion"};
        string result;
        int partNum[4];
        for (int i = 0; i < 4; ++i)
        {
            partNum[i] = num % 1000;
            num /= 1000;
            if (partNum[i] == 0) continue;
            result = stitch(partNum[i]) + unit[i] + result;
        }
        // 去掉result首位的空格
        return result.size() ? result.substr(1) : "Zero";
    }

private:
    string stitch(int num) {
        const string lessTen[10] = 
        {"", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"};
        const string lessTwenty[10] = 
        {" Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen",
        " Seventeen", " Eighteen", " Nineteen"};
        const string lessHundred[10] = 
        {"", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", 
        " Eighty", " Ninety"};

        string temp;

        if (num != 0)
        {
            int units, tens, hundreds; // 個位、十位、百位出現的
            hundreds = num / 100;
            tens = (num % 100) / 10;
            units = (num % 100) % 10;

            if (hundreds != 0)
                temp = temp + lessTen[hundreds] + " Hundred";

            if (tens != 0)
            {
                if (tens == 1)
                {
                    temp += lessTwenty[units];
                    return temp;
                }
                else
                    temp += lessHundred[tens];
            }

            if (units != 0)
                temp += lessTen[units];
        }
        return temp;
    }
};

這裡寫圖片描述

四. 小結

該題還是需要考慮很多邊界條件的,比如數字中出現0要如何表示。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved