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();
}
}

最新文章

  1. bzoj 1162 network
  2. 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
  3. Selenium2Library系列 keywords 之 _ElementKeywords
  4. 《linux性能及调优指南》
  5. D. Ilya and Escalator
  6. 题目1043:Day of Week(输入日期与当前日起天数差%7,在做相关星期调整)
  7. (原)windows8.1上使用opencv for python
  8. css动画过渡
  9. SQL-记录查询篇-009
  10. C#6.0语言规范(一) 介绍
  11. UITableView 的坑
  12. javascript void函数
  13. 词频统计 SPEC 20160911
  14. HDFS DATANODE 磁盘容量的最小值
  15. linux 基本配置tab键和显示行号 和中文输入法
  16. python模块之StringIO/cStringIO(内存文件)
  17. 服务器如何开启php的fsockopen函数? 使用发邮箱类
  18. 上传文件ie7
  19. mac 下 安装php扩展 - mcrypt
  20. object.Equals与object.ReferenceEquals方法

热门文章

  1. 《OD学oozie》20160813
  2. Codeforces Round #320 (Div. 2) D. &quot;Or&quot; Game 数学
  3. 纠结和郁闷的存在感-关于DirectX与HLSL的矩阵存储方式---转载好文章
  4. Android 用户界面---拖放(Drag and Drop)(三)
  5. Node Security
  6. 查找所有含有表名(abc)的存储过程 执行脚本
  7. Servlet的延迟加载和预加载
  8. HDU 1548 A strange lift 奇怪的电梯(BFS,水)
  9. 【Android】SDK工具学习 - Traceview 和 dmtracedump
  10. 使用 Linux 终端 SSH 登录 VPS