leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/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"
public class Solution { private final String[] c = {"Billion", "Million", "Thousand", ""};
private final String[] Tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private final String[] Basics = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private String getField(int idx, int size) {
if(size == 4) {
if(idx == 0) return "Billion";
else if(idx == 1) return "Million";
else if(idx == 2) return "Thousand";
else if(idx == 3) return "";
}
else if(size == 3) {
if(idx == 0) return "Million";
else if(idx == 1) return "Thousand";
else if(idx == 2) return "";
}
else if(size == 2) {
if(idx == 0) return "Thousand";
else if(idx == 1) return "";
}
return "";
} private LinkedList<String> read(int n) { LinkedList<String> rs = new LinkedList<String> (); int hundred = (n % 1000) / 100;
int ten = (n % 100) / 10;
int unit = n % 10; if(hundred > 0) {
rs.addLast(Basics[hundred]);
rs.addLast("Hundred");
}
if(ten > 0) {
if(ten == 1) {
int combo = ten * 10 + unit;
rs.addLast(Basics[combo]);
return rs;
} else {
rs.addLast(Tens[ten]);
}
}
if(unit > 0) {
rs.addLast(Basics[unit]);
}
return rs;
} public String numberToWords(int num) {
if(num == 0) {
return "Zero";
} LinkedList<Integer> d = new LinkedList<Integer> ();
while(num > 0) {
d.addFirst(num % 1000);
num/=1000;
} LinkedList<String> ls = new LinkedList<String> ();
for(int i=0; i<d.size(); ++i) {
if(d.get(i) == 0) continue; LinkedList<String> r = read(d.get(i));
String field = getField(i, d.size());
if(i == d.size() - 1) ls.addAll(r);
else {
ls.addAll(r);
ls.addLast(field);
}
}
StringBuffer rs = new StringBuffer();
for(int i=0; i<ls.size() - 1; ++i) {
rs.append(ls.get(i));
rs.append(" ");
}
rs.append(ls.peekLast());
return rs.toString().trim();
}
}
leetcode@ [273] Integer to English Words (String & Math)的更多相关文章
- [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 ...
- [leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- 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 ...
- 273. Integer to English Words数字转为单词
[抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
- [LC] 273. Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
随机推荐
- [LintCode]perfect-squares(DP)
题目链接:http://www.lintcode.com/zh-cn/problem/perfect-squares/ 就是求最小价值的完全背包,初始化dp[i]=i,假设全是1的时候是最多的,之后就 ...
- 【转载】Redis多实例及分区
主要看的这篇文章 http://mt.sohu.com/20160523/n451048025.shtml edis Partitioning即Redis分区,简单的说就是将数据分布到不同的redis ...
- 嵌入式ARM系统开发基础
从.net到delplhi 从windows到Linxu 未来有多远? 如何突破自己? 什么是自己? 我从哪里来,要到哪里去? 世界是什么? 是世选择了我,还是我选择了世界? 怎么才能够完成蜕变? 去 ...
- 查看mysql存储引擎
一般情况下,mysql会默认提供多种存储引擎,你可以通过下面的查看: 看你的mysql现在已提供什么存储引擎:mysql> show engines; 看你的mysql当前默认的存储引擎:mys ...
- 51nod1442 士兵的旅行
裸网络流拆点就可以了... #include<cstdio> #include<cstring> #include<cctype> #include<algo ...
- hdu 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- UVa 10474 Where is the Marble
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...
- 01.C语言关于结构体的学习笔记
我对于学习的C语言的结构体做一个小的学习总结,总结如下: 结构体:structure 结构体是一种用户自己建立的数据类型,由不同类型数据组成的组合型的数据结构.在其他高级语言中称为记录(record) ...
- Typed Message模式与Event Sourcing
引言 在<设计模式沉思录>(Pattern Hatching: Design Patterns Applied,[美]JohnVlissides著)一书的第4章中,围绕事件Message传 ...
- 【转】在Source Insight中看Python代码
原文网址:http://www.cnblogs.com/xuxm2007/archive/2010/09/02/1815695.html SI是个很强大的代码查看修改工具,以前用来看C,C++都是相当 ...