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 ...
随机推荐
- 14.allegro.PCB设计前工作[原创]
一.设置板子大小 -- ----- 板子边框 2种设置outline方法,创建2个KI,两个keepin,,r:允许布线区:p允许摆放元件的区域 法一:直接添加线 ①Board Geometry(最外 ...
- Html 修改placeholder的颜色属性css样式
项目需求需要修改文本框的placeholder 的文本颜色, 百度下, 备忘,我使用的是这种方法, ::-webkit-input-placeholder { /* WebKit browsers * ...
- leetcode:Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- hibernate annotation配置经验
1.将annotation写在entity类文件的get方法上面
- Win7平台下Cocos2d-x环境搭建
一.Win7下Cocos2d-x环境搭建 Cocos开发者平台官网——在这里下载游戏引擎 解压放到某个目录下即可 https://www.python.org/downloads/ ...
- Centos 6.5LAMP服务器(Apache+PHP+MySQL)的搭建
1.首先看下你的防火墙是否处于开启状态,如果是开启状态,按照如下方法来配置你的防火墙(如果你在安装虚拟机时就没有开启过防火墙,那么这一步就省略了): 1.配置防火墙,开启80端口.3306端口 vi ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- jquery 获取和设置 select下拉框的值(转手册)
##实例应用中遇到的问题 //在某事件响应的应用中设置select选中项,前两种情况的设置不生效,使用了最后一种用法才生效的 //$("#select_time").find(&q ...
- 如何寻找google公司主导的开源项目
在googlecode页面的搜索框中:搜索 label:google ,结果列表中就会显示所有开源软件列表.或者直接点击这个连接:http://code.google.com/hosting/sear ...
- 【英语】Bingo口语笔记(38) - See系列
see somebody off 送别 see somebody out 看到谁从哪里出来