[leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
Example 1:
Input: 123
Output: "One Hundred Twenty Three"
Example 2:
Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"
题目:
给定一个整数,像check一样将该数字用英文读出来
思路:
1. Coz given input is less than 231 - 1, we can say that the highest digit level is billion
2. We divide input into N parts, all the parts follow the similar partten of converting. We use helper function to help recursive call.
代码:
class Solution {
// coz array index begins at 0, so we use "" to empty a space
private final String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private final String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] belowHundred = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String numberToWords(int num) {
if (num == 0) return "Zero";
return helper(num);
}
private String helper(int num) {
String result = new String();
if (num < 10) result = belowTen[num];
else if (num < 20) result = belowTwenty[num -10];
// if num is smaller than 100, keep tens digit and add units digit
else if (num < 100) result = belowHundred[num/10] + " " + helper(num % 10);
// if num is smaller than thousand, call helper to get how many hundreds, call the helper to get the rest digits
else if (num < 1000) result = helper(num/100) + " Hundred " + helper(num % 100);
// if num is smaller than million, call helper to get how many thousands, call the helper to get the rest digits
else if (num < 1000000) result = helper(num/1000) + " Thousand " + helper(num % 1000);
// if num is smaller than billion, call helper to get how many millions, call the helper to get the rest digits
else if (num < 1000000000) result = helper(num/1000000) + " Million " + helper(num % 1000000);
else result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
return result.trim();
}
}
[leetcode]273. Integer to English Words 整数转英文单词的更多相关文章
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- LeetCode 273. Integer to English Words
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/description/ 题目: Convert a non-negati ...
- 273 Integer to English Words 整数转换英文表示
将非负整数转换为其对应的英文表示,给定的输入是保证小于 231 - 1 的.示例:123 -> "One Hundred Twenty Three"12345 -> & ...
- leetcode-【hard】273. Integer to English Words
题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...
- 【LeetCode】273. Integer to English Words
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- 【LeetCode】Integer to English Words 解题报告
Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...
- 273. Integer to English Words
题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...
随机推荐
- java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind 【 解决方案】
当我们在启动tomcat服务的时候报错信息:java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bin 分析:从错 ...
- git 常用命令--抓取分支-为自己记录(二)
二:抓取分支: 多人协作时,大家都会往master分支上推送各自的修改.现在我们可以模拟另外一个同事,可以在另一台电脑上(注意要把SSH key添加到github上)或者同一台电脑上另外一个目录克隆, ...
- 三网合一 中国移动铁通光猫 HG6821M 如何设置宽带自动连接
假期炎热,都说大连是海滨城市比较凉爽,但是那地方潮气太大,实在是不太好呆,于是乎我打道回府来到了内陆家中. 回到家中发现老式的光猫被替换成了 最新的 HG6821M . 按照铭牌上的说明,登录. ...
- 在create-react-app创建的项目下允许函数绑定运算符
前话 React的函数绑定一致是个问题,主要有下面几种方式: 事件处理器动态绑定 export default class Com extends React.Component { render() ...
- zedgraph控件怎么取得鼠标位置的坐标值(转帖)
我想取得zedgraph控件上任意鼠标位置的坐标值,IsShowCursorValues可以显示鼠标位置的值但是不能提取赋值给其他的变量.用PointValueEvent这个事件又只能得到已经画出的点 ...
- centos 6.5 下 安装 git
2018-07-02 centos安装Git有两种方式,一种通过源安装,通过如下命今,即可一键安装成功: yum -y install git 但是通过源安装的Git,目前最高版本是1.7,想要使用更 ...
- 64位windows 2003和windows xp
msdn windows2003 64位简体中文企业版R2 sp2(cn_win_srv_2003_r2_enterprise_x64_with_sp2_vl) ed2k://|file|cn_win ...
- vmware player 在windows下nat模式中的端口映射
1.设置虚拟机nat共享的网卡为固定ip vmware虚拟机使用nat网络时,是VMware Network Adapter VMnet8网卡提供的nat服务.查看VMware Network Ada ...
- php常量的实现
1.php的常量 就是一个简单的标识符,脚本执行期间不会改变,大小写敏感,默认大写. 常量的存储结构 typedef struct _zend_constant { zval value; //常量值 ...
- Flask框架的学习与实战(一):开发环境搭建
Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2.很多功能的实现都参考了django框架.由于项目需要,在 ...