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)的更多相关文章

  1. [LeetCode] 273. Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  2. LeetCode 273. Integer to English Words

    原题链接在这里:https://leetcode.com/problems/integer-to-english-words/description/ 题目: Convert a non-negati ...

  3. [leetcode]273. Integer to English Words 整数转英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

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

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

  5. 【LeetCode】273. Integer to English Words

    Integer to English Words Convert a non-negative integer to its english words representation. Given i ...

  6. 【LeetCode】Integer to English Words 解题报告

    Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...

  7. 273. Integer to English Words

    题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...

  8. 273. Integer to English Words数字转为单词

    [抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...

  9. [LC] 273. Integer to English Words

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

随机推荐

  1. RSA (cryptosystem)

    https://en.wikipedia.org/wiki/RSA_(cryptosystem) RSA is one of the first practical实用性的 public-key cr ...

  2. 第四篇 在中国做ERP系统实施你必须知道的一些常识

    1. ERP实施要特别从参与全球竞争的视角指引系统建设.中国社会经历了一个从计划经济体制到市场经济体制的转变.中国加入WTO后,要与国际接轨,要按照世界贸易组织有关的贸易规则开展国际贸易.中国的关税与 ...

  3. [NYIST15]括号匹配(二)(区间dp)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...

  4. JavaScript 类的定义和引用 JavaScript高级培训 自定义对象

    在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等.     一,概述 在Java语言中 ...

  5. 不同浏览器JS获取浏览器高度和宽度

    摘自:http://blog.csdn.net/lai_gb/archive/2009/07/04/4320956.aspx IE中: document.body.clientWidth ==> ...

  6. 【Todo】Java Queue Stack Vector ArrayList

    Java集合框架里存在Queue这个接口,之后有不同类型的队列的实现. 有Stack这个类实现堆栈,其实这个类是通过继承Vector的方式来实现的, Vector和ArrayList的实现方式差不多, ...

  7. 跨域利器JSONP(转)

    何为跨域?何为JSONP?JSONP技术能实现什么?是否有必要使用JSONP技术? 跨域 就是由于JavaScript同源策略的限制,使得a.com域名下的js无法操作b.com或c.a.com域名下 ...

  8. Node Security

    发一个很早之前做的一个小东西-安全管理软件-可以对U盘进行管理,对后台程序进行扫描.分析!

  9. (转载) jQuery 页面加载初始化的方法有3种

    jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...

  10. 为什么要用Java泛型

    啥是泛型? 泛型(generic)是指参数化类型的能力.可以定义带泛型类型的类或方法,随后编译器会用具体的类型来代替它. 举个栗子 上述代码在编译期没有问题,但在运行期,将会报错.就是因为List的a ...