[LC] 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"
class Solution {
String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
String[] THOUSANDS = {"", "Thousand", "Million", "Billion"};
public String numberToWords(int num) {
if (num == 0) {
return "Zero";
}
String res = "";
int i = 0;
while (num > 0) {
if (num % 1000 != 0) {
res = helper(num % 1000) + THOUSANDS[i] + " "+ res;
}
num /= 1000;
i += 1;
}
return res.trim();
}
private String helper(int num) {
if (num == 0) {
return "";
}
if (num < 20) {
return LESS_THAN_20[num] + " ";
} else if (num < 100) {
return TENS[num / 10] + " " + helper(num % 10);
} else {
return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100);
}
}
}
[LC] 273. Integer to English Words的更多相关文章
- 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 ...
- 273. Integer to English Words数字转为单词
[抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
- 273. Integer to English Words
题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...
- 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 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] 273. 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
原题链接在这里: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 -> & ...
随机推荐
- Bootstrap-按钮篇btn
参考网址:http://v3.bootcss.com/(能抄不写) 1.按钮颜色样式: 对应代码:(主要体现在class内容:btn-default,btn-primary...) <butto ...
- 洛谷 P2196 挖地雷
题目传送门 解题思路: 记忆化搜索,题目比较坑的地方在于,这是个有向图,给的边是单向边!!!!!!!! AC代码: #include<iostream> #include<cstdi ...
- bzoj4316小C的独立集(dfs树/仙人掌+DP)
本题有两种写法,dfs树上DP和仙人掌DP. 先考虑dfs树DP. 什么是dfs树?其实是对于一棵仙人掌,dfs后形成生成树,找出非树边(即返祖边),然后dfs后每条返祖边+其所覆盖的链构成了一个环( ...
- Ubuntu无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它?
转自:http://hi.baidu.com/xiaobaishu 鸣谢: xuleibjtu2010的原创空间 1.终端输入 ps -aux ,列出进程.找到含有apt‘-get的进程,直接sud ...
- Maven:程序包不存在,找不到符号
maven build时出现了以下的错误: 程序包xx.xx不存在,xxx找不到符号 原因一:DAO层依赖Service接口层的Bean类,在pom.xml中添加了对Service的依赖,也因此引入了 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:Number 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Aras Innovator获取项目任务序列号
//方法名:GetProjectTasksNumber //功能描述:获取项目任务序列号 //原作者:joe //创建时间:20141225 //版权所有(C)JOE.FAN //---------- ...
- 吴裕雄--天生自然 JAVA开发学习:String 类
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...
- UML-迭代1-基础
1.原则 1).拆分成多个按时间定量.风险驱动的迭代(细化1.细化2.细化3.细化4) 2).在多个迭代中,对同一用例增量开发. 2.最佳实践 设计人员:对架构的核心和风险做适当设计.实现和测试 研发 ...
- NWERC 2015
2015-2016 Northwestern European Regional Contest (NWERC 2015) F H没做 似乎只有 B 题有点意思 D:数论分块枚举所有上取整区间,只需要 ...