【LeetCode】273. 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"
class Solution {
public:
string Helper(string numStr, string dict1[], string dict2[], string dict3[])
{
int num = atoi(numStr.c_str());
if(num == )
return "";
else
{
string ret = "";
int hundred = numStr[] - '';
int ten = numStr[] - '';
int one = numStr[] - '';
if(hundred != )
ret += dict1[hundred-] + " Hundred";
if(ten == )
{
if(one == )
return ret;
else
return (ret=="")?(dict1[one-]):(ret+" "+dict1[one-]);
}
else if(ten == )
{
return (ret=="")?(dict2[one]):(ret+" "+dict2[one]);
}
else
{
if(one == )
return (ret=="")?(dict3[ten-]):(ret+" "+dict3[ten-]);
else
return (ret=="")?(dict3[ten-]+" "+dict1[one-]):(ret+" "+dict3[ten-]+" "+dict1[one-]);
}
}
}
string numberToWords(int num) {
if(num == )
return "Zero";
else
{
string ret = "";
string dict1[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
string dict2[] = {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
"Eighteen","Nineteen"};
string dict3[] = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
string numStr = to_string(num);
int len = numStr.size();
string padding = string(-len, '');
numStr = padding + numStr;
string billionStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(billionStr != "")
ret += billionStr + " Billion";
string millionStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(millionStr != "")
ret += (ret=="")?(millionStr+" Million"):(" "+millionStr+" Million");
string thousandStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(thousandStr != "")
ret += (ret=="")?(thousandStr+" Thousand"):(" "+thousandStr+" Thousand");
string oneStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(oneStr != "")
ret += (ret=="")?(oneStr):(" "+oneStr);
return ret;
}
}
};

【LeetCode】273. Integer to English Words的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- leetcode-【hard】273. Integer to English Words
题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reverse Integer
题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...
- 【LeetCode】12. Integer to Roman 整型数转罗马数
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- 【leetcode】12. Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
随机推荐
- java多条件不定条件查询
网站或各类管理系统都会用到搜索,会用到一个或多个不确定条件搜索,单条件搜索比较简单,有时候会有多个条件共同查询,如果系统中已经提供了相关的方法供你使用最好,像我做这老系统改版,需要添加搜索,就要自己写 ...
- MYSQL 查询出最大/最小值所在的记录
基本上都知道用MAX()/MIN()来求出所需的最大/最小值,但是只能查出那个最值的字段,而想查出整条记录或是对应的其他值却不行(SELECT MAX(grade), name FROM test;- ...
- MySQL关键性能监控(QPS/TPS)
原文链接:http://www.cnblogs.com/chenty/p/5191777.html 工作中尝尝会遇到各种数据库性能调优,除了查看某条SQL执行时间长短外,还需要对系统的整体处理能力有更 ...
- 【kd-tree】bzoj4154 [Ipsc2015]Generating Synergy
区间修改的kd-tree,打标记,下传. 每次询问的时候,从询问点向上找到根,然后依次下传下来,再回答询问. #include<cstdio> #include<algorithm& ...
- java中this用法总结
1,当局部变量和成员变量重名的时候,在方法中使用this表示成员变量以示区分. class Demo{ String str = "这是成员变量"; void fun(String ...
- Android学习整理之Activity生命周期篇
一.Activity生命周期说明 Activity的四种状态: ⒈活动状态(Active or Running):也称为运行状态,处于Activity栈顶,在用户界面中最上层,完全能被用户看到,能 ...
- fancybox iframe 刷新父页面(项目经验)
<script type="text/javascript"> $(document).ready(function() { $(".fancybox&quo ...
- Lzlib 1.5 正式发布,C 语言压缩算法库
Lzlib 1.5 正式发布,该版本移除了对废弃版本 0 文件的解压支持:修复了 struct LZ_Encoder 和 LZ_compress_sync_flush 相关的 bug. Lzlib 压 ...
- 基于QT的webkit与ExtJs开发CB/S结构的企业应用管理系统
一:源起 1.何为CB/S的应用程序 C/S结构的应用程序,是客户端/服务端形式的应用程序,这种应用程序要在客户电脑上安装一个程序,客户使用这个程序与服务端通信,完成一定的 ...
- Devexpress GridView内嵌dx:ASPxGridLookup取得控件值乱跳解决方案
一 页面嵌入ASPxGridLookup控件 <dx:ASPxGridView ID="gridDetail" runat="server" ...