Integer to English Words -- leetcode
来源:互联网 发布:淘宝砍价技巧 编辑:程序博客网 时间:2024/05/16 12:06
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"
基本思路:
需要特殊处理的基本数字是,1~19,
然后就是整十,如,20,30,40,,,90
然后要注意是空格的添加。
class Solution {public: string numberToWords(int num) { if (!num) return "Zero"; return numberToEnglish(num).substr(1); } string numberToEnglish(int num) { static const char *oneTo19Map[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; static const char *tensMap[] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; if (num >= 1000000000) return numberToEnglish(num / 1000000000) + " Billion" + numberToEnglish(num % 1000000000); else if (num >= 1000000) return numberToEnglish(num / 1000000) + " Million" + numberToEnglish(num % 1000000); else if (num >= 1000) return numberToEnglish(num / 1000) + " Thousand" + numberToEnglish(num % 1000); else if (num >= 100) return numberToEnglish(num / 100) + " Hundred" + numberToEnglish(num % 100); else if (num >= 20) return string(" ") + tensMap[num/10] + numberToEnglish(num % 10); else if (num) return string(" ") + oneTo19Map[num]; else return ""; }};
最后将自己的第一版也贴上作个纪念:
class Solution {public: string numberToWords(int num) { const char *thousandsMap[] = {"", " Thousand", " Million", " Billion"}; int base = 1; int index = 0; while (num / base >= 1000) { ++index; base *= 1000; } string ans; while (base) { string item = numberToWordsUnit(num / base); if (!item.empty()) { if (!ans.empty()) ans.push_back(' '); ans += item + thousandsMap[index]; } num %= base; base /= 1000; --index; } return ans.empty() ? "Zero" : ans; } string numberToWordsUnit(int num) { const char *OneTo19[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; const char *decadeMap[] = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; string ans; const int hundred = num / 100; num %= 100; if (hundred) ans += string(OneTo19[hundred]) + " Hundred"; if (!num) return ans; if (!ans.empty()) ans.push_back(' '); if (num < 20) { ans += OneTo19[num]; } else { const int decade = num / 10; num %= 10; ans += decadeMap[decade]; if (!num) return ans; if (!ans.empty()) ans.push_back(' '); ans += OneTo19[num]; } return ans; }};
0 0
- leetcode - Integer to English Words
- [leetcode] Integer to English Words
- Integer to English Words -- leetcode
- [LeetCode] Integer to English Words
- LeetCode -- Integer to English Words
- 【LeetCode】Integer to English Words
- LeetCode Integer to English Words
- Leetcode: Integer to English Words
- leetcode:Integer to English Words
- 【LeetCode】Integer to English Words
- [leetcode] 273.Integer to English Words
- [leetcode-273]Integer to English Words(java)
- [leetcode 273]Integer to English Words
- LeetCode-Integer to English Words-解题报告
- leetcode刷题-Integer to English Words
- LeetCode之Integer to English Words
- 【LeetCode】Integer to English Words 解题报告
- LeetCode 题解(210) : Integer to English Words
- php multi_query()函数
- DirectX11 深度/模板状态
- 第四周 项目1-建立单链表
- linux vim 笔记(部分取自vim编译器资料)
- 关于sizeof的题目
- Integer to English Words -- leetcode
- Web前端性能优化之浏览器访问优化
- 黑盒测试
- Java内存分析 --- 虚拟机运行时数据区
- Android开发之基础新特性-------------Fragment&帧动画(一知识点)
- poj3299解题报告
- 网络与并发
- 3dmt项目开发总结
- 求1+2+3+...+n