【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 ...
随机推荐
- Python缩进
今天练习代码的时候发现一个问题,练习类,我在notepad++上写的代码运行后,复制到pycharm上运行然后报错,看代码 #---coding:utf-8--- #定义一个Person类然后实例化 ...
- mysql 常用配置
1. 帐号不允许从远程登陆,只能在localhost 这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从“loc ...
- Ubuntu下制作ISO文件
利用Ubuntu自带的命令mkisofs就可以制作iso文件,具体方法如下: 1. 如果你是直接从cd压制iso文件的,执行 sudo umount /dev/cdromdd if=/dev/cd ...
- ymPrompt消息组件
<script src="jb51.net/prompt/jquery-1.7.1.js" type="text/javascript"></ ...
- GROUP BY,WHERE,HAVING之间的区别和用法
GROUP BY,WHERE,HAVING之间的区别和用法 分类: Oracle学习2009-11-01 23:40 21963人阅读 评论(6) 收藏 举报 mathmanagersql数据库m ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- mysql 5.6并行复制事件分发机制
并行复制相关线程 在MySQL 5.6并行复制中,当设置set global slave_parallel_workers=2时,共有4个复制相关的线程,如下: +----+------------- ...
- Windows Server 2008 64 位 IIS7.5 ASP.NET MVC4 发布问题
问题描述: 环境与配置: ASP.NET MVC 4 WINDOWS SERVER 2008 64 位 应用程序池是选择的 .NET 4.0 与经典模式 在新建一个MVC 4 项目发现到服务器上后 ...
- JsCss笔记
1. &= 不应该在 Bool 型变量中使用. a &= b 对于Js来说是: a = a & b ; a 本来是 bool , &= 之后就变成了 Int. ...
- 在Win7 环境使用Java API 上传文件到 Hadoop2.x HDFS 问题统计
问题一: org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security .AccessControlExc ...