leetcode-【hard】273. Integer to English Words

題目:less

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.

For example,ide

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

答案:spa

按着本身用英文閱讀整數的順序來就行:3d

一、這道題目不是難在思路上,而是難在考慮的狀況比較多,比較多細節,一不當心就有小bug,須要思路清晰,邏輯清晰code

二、英文中用來表示整數的單詞並很少,分類:blog

1-9(個位數),11-19(特殊的十位數),10-90(十位數),100(hundred),1000(thousand),1000000(million),1000000000(billion)three

三、10這個十位數比較特別,只有在後兩位徹底爲10時,才用tenrem

四、注意空格,後面沒有數字了,就不能加空格了input

五、每千位數跟後面的千位數(若是不爲空,即0000000……)要有空格string

六、注意不要亂加前綴空格和後綴空格

將上面的細節注意了就AC啦

代碼:

  1 #include <vector>
  2 #include <string>
  3 
  4 using std::vector;
  5 using std::string;
  6 
  7 string n2s[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
  8 string g2s[] = {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
  9 string t2s[] = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
 10 
 11 class Solution {
 12 private:
 13     vector<string> ans;
 14     
 15 public:
 16     string numberToWords(int num) {
 17         if(num == 0)
 18         {
 19             return "Zero";
 20         }
 21         
 22         unsigned int rem;
 23         unsigned int one;
 24         unsigned int two;
 25         unsigned int three;
 26         
 27         string perAns;
 28         ans.clear();
 29         
 30         do
 31         {
 32             //獲取餘數
 33             rem = num % 1000;
 34             num = num /1000;
 35             
 36             //存儲每次計算的結果
 37             perAns.clear();
 38             perAns = "";
 39             
 40             //一千之內的數,取每一位
 41             one = rem % 10;
 42             two = (rem / 10) % 10;
 43             three = (rem / 100) % 10;
 44             
 45             if(three != 0)
 46             {
 47                 perAns += n2s[three - 1];
 48                 perAns += " Hundred";
 49             }
 50             
 51             //若是後兩位都爲0,那麼後面就沒有數了,也就不須要加空格
 52             if(perAns != "" && (one != 0 || two != 0))
 53             {
 54                 perAns += " ";
 55             }
 56             
 57             if(two == 1)
 58             {
 59                 //考慮最後一位是否爲0的狀況,因最後兩位爲10時,須要用「ten」
 60                 //不然就是十位數
 61                 if(one == 0)
 62                 {
 63                     perAns += t2s[0];
 64                 }else
 65                 {
 66                     perAns += g2s[one - 1];
 67                 }
 68             //考慮two不爲0,不爲1,則按通常規則去計算
 69             }else if(two != 0)
 70             {
 71                 perAns += t2s[two - 1];
 72                 
 73                 if(one != 0)
 74                 {
 75                     perAns += " ";//若是最後一位不爲0,須要在其前面加空格
 76                     perAns += n2s[one - 1];
 77                 }
 78             //考慮two爲0的狀況
 79             }else
 80             {
 81                 if(one != 0)
 82                 {
 83                     perAns += n2s[one - 1];
 84                 }
 85             }
 86             
 87             //將結果存儲到ans中,ans中的答案是以逆序形式存儲了每一個千位數
 88             ans.push_back(perAns);
 89         }while(num != 0);
 90         
 91         string result = "";
 92         unsigned int len = ans.size();
 93         //len最大長度爲4,考慮每種位數就行,這裏用的時候就會發現,合理使用goto,程序邏輯會很清晰
 94         switch(len)
 95         {
 96             case 4:goto three;break;
 97             case 3:goto two;break;
 98             case 2:goto one;break;
 99             case 1:goto zero;break;
100         }
101         
102         three:
103         if(ans[3] != "")
104         {
105             result += ans[3];
106             result += " Billion";
107             
108             if(ans[2] != "" || ans[1] != "" || ans[0] != "")
109             {
110                 result += " ";
111             }
112         }
113         
114         two:
115         if(ans[2] != "")
116         {
117             result += ans[2];
118             result += " Million";
119             
120             if(ans[1] != "" || ans[0] != "")
121             {
122                 result += " ";
123             }
124         }
125         
126         one:
127         if(ans[1] != "")
128         {
129             result += ans[1];
130             result += " Thousand";
131             
132             if(ans[0] != "")
133             {
134                 result += " ";
135             }
136         }
137         
138         zero:
139         if(ans[0] != "")
140         {
141             result += ans[0];
142         }
143         
144         return result;
145     }
146 };
View Code

說明: 合理使用goto會取到很好的效果哦

相關文章
相關標籤/搜索