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"

Show Hint

 先把数字分割成4个三分位处理。前3个三分位分别加上Billion,Million,Thousand。
三分位处理函数为Helper()
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的更多相关文章

  1. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  2. leetcode-【hard】273. Integer to English Words

    题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...

  3. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  4. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  5. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  6. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  7. 【leetcode】Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...

  8. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  9. 【leetcode】12. Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

随机推荐

  1. Jplayer歌词同步显示插件

    http://blog.csdn.net/wk313753744/article/details/38758317 1.该插件是一个jquery的编写的跟jplayer实现歌词同步的插件,最终效果如图 ...

  2. 如何利用java得到当前的时间和前一天的时间

    1.得到当前的时间 Calendar   cal   =   Calendar.getInstance(); String time=formatter.format(cal.getTime()); ...

  3. svn从windows迁移到mac

    http://my.oschina.net/grnick/blog/161424 一.备份VisualSVN项目 1. 现在要使用Linux作为svn服务器,之前是在windows Server 20 ...

  4. Storm启动流程简介

    storm启动流程          storm是一个流行的开源的,分布式实时处理框架,关于storm的基本介绍可以参加这篇官方文档.大致的拓扑结构如图所示:        其中Nimbus是一个后台 ...

  5. C# async await 学习笔记1

    由于我的开发工具为vs.net 2010(.net 4.0),需先做以下两步才能进行: 1.下载并安装Async CTP (http://www.microsoft.com/en-us/downloa ...

  6. 虚拟机Ubuntu14/15启用root用户登录

    1. 为root用户设置密码 sudo passwd root 需要先输入一次当前用户的登陆密码,然后输入两次root用户的密码(自己设置). 2. 切换至root用户 sudo -s 3. 编辑登陆 ...

  7. Linux: shell常用通配符

    字符 含义 * 匹配 0 或多个字符 ? 匹配任意一个字符 [list] 匹配 list 中的任意单一字符 [!list] 匹配 除list 中的任意单一字符以外的字符 [c1-c2] 匹配 c1-c ...

  8. Windows Azure 使用体验

    本文只是对Windows Azure的肤浅使用做个记录,算是简单入门吧. 一.门户网站 Windows Azure其实有两个版本,我们在国内所说的或者说所用的就是有别于国际版的,主要原因我想各位也是知 ...

  9. thrift之TTransport层的缓存传输类TBufferedTransport和缓冲基类TBufferBase

    本节主要介绍缓冲相关的传输类,缓存的作用就是为了提高读写的效率.Thrift在实现缓存传输的时候首先建立一个缓存的基类,然后需要实现缓存功能的类都可以直接从这个基类继承.下面就详细分析这个基类以及一个 ...

  10. AngularJS快速入门指南03:表达式

    AngularJS通过表达式将数据绑定到HTML. AngularJS表达式 AngularJS表达式写在双大括号中:{{ 表达式语句 }}. AngularJS表达式绑定数据到HTML的方式与ng- ...