LeetCode Integer to English Words
原题链接在这里: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.
Example 1:
Input: 123
Output: "One Hundred Twenty Three"
Example 2:
Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Example 4:
Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
题解:
没三个digit分成一个 unit, 用unitNumber 函数把这三位数换算成数字加上对应的unit.
Note: num = 1,000,000时不可以出现 "One Million Thousand"的情况,所以while循环时 要加上if(rest>0)的限定条件.
Time Complexity: O(n), n是unit的个数 Space: O(1).
AC Java:
public class Solution {
public String numberToWords(int num) {
if(num == 0){
return "Zero";
}
String res = "";
String [] units = {"", " Thousand", " Million", " Billion"};
int i = 0;
while(num > 0){
int unit = num%1000;
if(unit > 0){ //error 1,000,000
res = unitNumber(unit) + units[i] + (res.length() == 0 ? "" : " "+res);
}
num = num/1000;
i++;
}
return res;
} private String unitNumber(int unit){
String res = "";
String [] ten = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String [] twenty ={"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
String [] hundred = {"Zero","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; int a = unit/100;
int b = unit%100;
int c = unit%10; if(a > 0){
res = ten[a] + " Hundred";
} if(b>=10 && b<20){
if(res.length() != 0){
res = res + " ";
}
res = res + twenty[b%10];
return res;
}else if(b >= 20){
if(res.length() != 0){
res = res + " ";
}
b = b/10;
res = res + hundred[b];
} if(c > 0){
if(res.length() != 0){
res = res + " ";
}
res = res + ten[c];
}
return res;
}
}
LeetCode Integer to English Words的更多相关文章
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- 【LeetCode】Integer to English Words 解题报告
Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...
- 【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 Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 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 (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [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 ...
- Integer to English words leetcode java
问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
随机推荐
- 清除 WD MyCloud 自动生成的 .wdmc 目录
1. 先 SSH,停止相应服务 /etc/init.d/wdmcserverd stop/etc/init.d/wdphotodbmergerd stop 2. 禁止服务自启动 update-rc.d ...
- Linux下java获取CPU、内存、磁盘IO、网络带宽使用率
一.CPU 使用proc文件系统,"proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为访问系统内核数据的操作提供接口.用户和应用程序可以通过proc得 ...
- 关于HTML条件注释你可能不知道的一些事儿
最近经常看到类似这样的HTML代码片段,很多前端开发人员应该都熟悉: 1 <!--[if lt IE 7]> <html class="ie6"> ...
- lintcode :Coins in Line II 硬币排成线 II
题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是 ...
- git实用攻略
Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 下面是实用教程,免基础. 一.安装git https://git-scm.com/downloads 二.git创建 ...
- Bloomberg SEP 12.x 迁移小记
备份 个文件: D:\Program Files\Symantec\Symantec Endpoint Protection Manager\Server Private Key Backup ...
- NSQ:分布式消息队列学习记录
参考资料: NSQ:分布式的实时消息平台 初识NSQ分布式实时消息架构 深入NSQ之旅 nsq topic和channel的区别
- 《Pro Git》笔记3:分支基本操作
<Pro Git>笔记3:Git分支基本操作 分支使多线开发和合并非常容易.Git的分支就是一个指向提交对象的可变指针,极其轻量.Git的默认分支为master. 1.Git数据存储结构和 ...
- [ZZ] 在windows上编译Mesa3d opengl32库
在windows上编译Mesa3d opengl32库 cheungmine http://blog.csdn.net/ubuntu64fan/article/details/8061475 Mesa ...
- Nginx 笔记与总结(10)Nginx 与 PHP 整合
Apache + PHP 的编译 和 Nginx + PHP 的编译,区别: Apache 一般把 PHP 当作自己的一个模块来启动: Nginx 则是把 HTTP 请求变量(如 get,user_a ...