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. 安装hma master出错 Error: Package: perl-Mail-Sender-0.8.13-2.el5.1.noarch

    You are using the EPEL 5 version of the repo instead of 6, go into your /etc/yum.repos.d/epel.repo f ...

  2. ACdream 1726 A Math game (dfs+二分)

    http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...

  3. Android内存管理(3)缓存不要用SoftReference, 用android.util.LruCache

    A reference that is cleared when its referent is not strongly reachable and there is memory pressure ...

  4. leetcode:House Robber(动态规划dp1)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  5. pl/sql programming 06 异常处理

    如果 PLSQL发生了错误, 无论是系统错误还是应用错误, 都会抛出一个异常, 当前 PL/SQL 块中执行单元会暂停处理, 如果当前块有一个异常处理单元的话, 控制会转移到当前块的异常处理单元来处理 ...

  6. Tomcat,Jboss,Glassfish等web容器比较选型

    概述 Web容器是一种服务调用的规范,J2EE运用了大量的容器和组件技术来构建分层的企业应用.在J2EE规范中,相应的有WEB Container和EJB Container等. Web容器给处于其中 ...

  7. bzoj4127

    肯定是树链剖分+线段树,关键是怎么维护 绝对值和这个东西显然不能简单的合并标记 因为对于负数,加之后绝对值和是变小的 那我们考虑对负数和非负数数分别维护 下面的问题就是经过操作如果负数变成了正数怎么办 ...

  8. asp.net下通过泛解析和伪静态实现二级域名的实现方法

    在net中微软已经为我们留下了接口,让我们为所欲为了. 首先我们可以通过一张图大概了解下.net的生命周期. 从 上图可以看出来,针对每个不同用户的请求,服务器都会创建一个新的HttpContext实 ...

  9. const,readonly 常量与只读

    Const是常量 Const在编译时会被编译为静态成员,它确定于编译时期,属类型级,通过类型来访问. 现在通过以下几种情况来说明const常量: (1)初始化 public const string  ...

  10. R2的版本由来

    给人一杯水,自己先有一桶水.上课.备课,那么备课中就常有一些稀奇古怪的问题. 学生问:SP2和R2是一样的吗? 老师答:不一样,一个是补丁程序,另一个是服务器操作系统. 学生不解:R2如果是操作系统, ...