Re: [閒聊] 每日leetcode

看板Marginalman作者 (是oin的說)時間1年前 (2024/08/07 13:06), 編輯推噓1(102)
留言3則, 3人參與, 1年前最新討論串658/1549 (看更多)
※ 引述 《dont (dont)》 之銘言: :   : 273. Integer to English Words :   : ## 思路 :   : 列出所有可能的英文字 : 1~19, 十位數, 百/千/百萬/十億 :   : Input: num = 1234567 : Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty : Seven" :   : 1,234,567 = 1 Million + 234 Thousand + 567 : 234 = 2 Hundred + 30 + 4 : 567 = 5 Hundred + 60 + 7 :   : 把數字照10**9, 10**6, 10**3, 100順序切割做recursion轉換成英文 :   : ## Code 思路: 因為jiwp很有錢 所以把錢給他處理 然後照題目硬做 這題真的好機掰 class Solution { public: unordered_map<int,string> save; string jiwp(string n) { int now = stoi(n); string res ; if(save[now/100] != "Zero") { res+=save[(now/100)]; res+=" Hundred"; } if(now%100>0) { if(!res.empty())res+=" "; if(now%100 <20) { res+=save[now%100]; } else { res += save[(now % 100) - (now % 10)]; if (now % 10 != 0) { res += " "; res += save[now % 10]; } } } return res; } string numberToWords(int num) { string nums = to_string(num); if (num == 0) return "Zero"; save = { {0, "Zero"}, {1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"}, {5, "Five"}, {6, "Six"}, {7, "Seven"}, {8, "Eight"}, {9, "Nine"}, {10, "Ten"}, {11, "Eleven"}, {12, "Twelve"}, {13, "Thirteen"}, {14, "Fourteen"}, {15, "Fifteen"}, {16, "Sixteen"}, {17, "Seventeen" }, {18, "Eighteen"}, {19, "Nineteen"}, {20, "Twenty"}, {30, "Thirty"}, {40, "Forty"}, {50, "Fifty"}, {60, "Sixty"}, {70, "Seventy"}, {80, "Eighty"}, {90, "Ninety"} }; int len = nums.size(); string res ; if (len > 9) { res += jiwp(nums.substr(0, len - 9)); res += " Billion"; nums = nums.substr(len - 9); len = nums.size(); } if (len > 6) { if (!res.empty()&&res[res.size()-1] != ' ') res += " "; res += jiwp(nums.substr(0, len - 6)); if(jiwp(nums.substr(0, len - 6)) != "" )res += " Million"; nums = nums.substr(len - 6); len = nums.size(); } if (len > 3) { if (!res.empty()&&res[res.size()-1] != ' ') res += " "; res += jiwp(nums.substr(0, len - 3)); if(jiwp(nums.substr(0, len - 3)) != "" ) res += " Thousand"; nums = nums.substr(len - 3); len = nums.size(); } if (len > 0) { if (!res.empty()&&res[res.size()-1] != ' ') res += " "; res += jiwp(nums.substr(0, len)); } while(res[res.size()-1] == ' ')res.pop_back(); return res; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.129.159 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723007170.A.52A.html

08/07 13:06, 1年前 , 1F
你有什麼用
08/07 13:06, 1F

08/07 13:12, 1年前 , 2F
你有什麼用
08/07 13:12, 2F

08/07 13:14, 1年前 , 3F
你有甚麼用
08/07 13:14, 3F
文章代碼(AID): #1cim32Kg (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cim32Kg (Marginalman)