[LeetCode][JavaScript]Integer to English Words

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)

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

 

 


 

 

这题有毒,边界情况很多。

解法是递归处理千位以内的数,最后在后面加上"Thousand", "Million", "Billion"。

处理三位数的时候,先处理百位,剩下两位的数。

两位数又是两种,20以内的直接打表,21到99还要拆成个位和十位。

 

需要注意:

1. 零

2. 数字之间的空格

3. 1000000 --> One Million, 100000 --> One Hundred Thousand

 

 1 /**
 2  * @param {number} num
 3  * @return {string}
 4  */
 5 var numberToWords = function(num) {
 6     var dict = {};
 7     dict[0]= "Zero"; dict[1]= "One"; dict[2]= "Two"; dict[3]= "Three"; dict[4]= "Four"; dict[5]= "Five"; dict[6]= "Six";
 8     dict[7]= "Seven"; dict[8]= "Eight"; dict[9]= "Nine"; dict[10]= "Ten"; dict[11]= "Eleven"; dict[12]= "Twelve"; 
 9     dict[13]= "Thirteen"; dict[14]= "Fourteen"; dict[15]= "Fifteen"; dict[16]= "Sixteen"; dict[17]= "Seventeen"; 
10     dict[18]= "Eighteen"; dict[19]= "Nineteen"; dict[20]= "Twenty"; dict[30]= "Thirty"; dict[40]= "Forty"; dict[50]= "Fifty";
11     dict[60]= "Sixty"; dict[70]= "Seventy"; dict[80]= "Eighty"; dict[90]= "Ninety"; dict[100]= "Hundred"; 
12     var dict2 = ["Thousand", "Million", "Billion"];
13     
14     if(num === 0){
15         return dict[0];
16     }
17     var res = "", c = -1;
18     while(num !== 0){
19         lessThanThousand(num % 1000);
20         num = parseInt(num / 1000);
21         c++;
22     }
23     return res.trim();
24     
25     function lessThanThousand(n){
26         var str = "", count = 0;
27         //100 - 999
28         if(n >= 100){
29             count = parseInt(n / 100);
30             n = n % 100;
31             str += dict[count] + " " + dict[100];
32         }
33         //1 - 99
34         if(n !== 0){
35             if(str !== ""){
36                 str += " ";
37             }
38             if(n <= 20){  //1 - 20
39                 str += dict[n];
40             }else{ //21 -99
41                 var unitDigit = n % 10;
42                 n = n - unitDigit;
43                 str += unitDigit === 0 ? dict[n] : dict[n] + " " + dict[unitDigit];
44             }
45         }
46         //"Thousand", "Million", "Billion"
47         if(c >= 0 && str !== ""){
48             str += " " + dict2[c] + " ";
49         }
50         res = (str + res).trim();
51     }
52 };

 

 

posted @ 2015-09-01 14:50  `Liok  阅读(574)  评论(0编辑  收藏  举报