273 Integer to English Words 整数转换英文表示
將非負(fù)整數(shù)轉(zhuǎn)換為其對(duì)應(yīng)的英文表示,給定的輸入是保證小于 231?- 1?的。
示例:
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
詳見:https://leetcode.com/problems/integer-to-english-words/description/
Java實(shí)現(xiàn):
每三位數(shù)一讀。每讀三位數(shù)的時(shí)候在三位數(shù)后面加上單位(Thousand, Million, or Billion)即可。其中的關(guān)鍵點(diǎn)是當(dāng)讀取的三位數(shù)是“0”,那么后面的單位就要舍棄。比如1000000,讀第二個(gè)三位數(shù)是“000”,那么對(duì)應(yīng)的單位Thousand是要舍棄的,否則就會(huì)變成One Million Thousand的錯(cuò)誤結(jié)果。
class Solution {//全局變量先把要用的英文存起來String[] units = {"", " Thousand", " Million", " Billion"};String[] num0To9 = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};String[] num10To19 = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};String[] num10To90 = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};public String numberToWords(int num) {if (num == 0){return "Zero";}String res = ""; int count = 0; //記錄當(dāng)前三位數(shù)下后面跟的單位while (num > 0) {String tmp = "";tmp = units[count]; //記錄當(dāng)前三位數(shù)下后面跟的單位int cur = num % 1000; //每三位一讀,從后往前String pre = convert(cur); //轉(zhuǎn)化當(dāng)前數(shù)字最后的三位數(shù)if (pre == ""){tmp = ""; //如果是"000",那么就等于什么都沒發(fā)生,舍棄單位}else{tmp = convert(cur) + tmp; //否則結(jié)合結(jié)果和單位}if (res.length() != 0 && res.charAt(0) != ' '){ //處理一下加上單位的空格情況res = tmp + " " + res;}else{res = tmp + res;}num = (num - num % 1000) / 1000; //處理往前三位數(shù)count++;}return res;}//轉(zhuǎn)化任意三位數(shù)public String convert(int num) {if (num == 0){ return "";}if (num < 10){return num0To9[num];}else if (num >= 10 && num <= 19){ return num10To19[num - 10];}else if (num >= 20 && num <= 99) {if (num % 10 == 0){return num10To90[num / 10 - 1];}else{String s1 = num0To9[num%10];String s2 = num10To90[num/10 - 1];return s2 + " " + s1;}}else {if (num % 100 == 0){return num0To9[num / 100] + " Hundred";}else {String tmp = convert(num % 100);return convert(num - num % 100) + " " + tmp;}} } }C++實(shí)現(xiàn):
class Solution { public:string numberToWords(int num) {string res = convertHundred(num % 1000);vector<string> v = {"Thousand", "Million", "Billion"};for (int i = 0; i < 3; ++i) {num /= 1000;res = num % 1000 ? convertHundred(num % 1000) + " " + v[i] + " " + res : res;}while (res.back() == ' '){res.pop_back();}return res.empty() ? "Zero" : res;}string convertHundred(int num) {vector<string> v1 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};vector<string> v2 = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};string res;int a = num / 100, b = num % 100, c = num % 10;res = b < 20 ? v1[b] : v2[b / 10] + (c ? " " + v1[c] : "");if (a > 0) {res = v1[a] + " Hundred" + (b ? " " + res : "");}return res;} };?參考:https://www.cnblogs.com/grandyang/p/4772780.html
轉(zhuǎn)載于:https://www.cnblogs.com/xidian2014/p/8761300.html
總結(jié)
以上是生活随笔為你收集整理的273 Integer to English Words 整数转换英文表示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 239 Sliding Window M
- 下一篇: 课堂练习---统计空格流程图、Jacks