原题链接在这里: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的更多相关文章

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

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

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

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

  3. 【LeetCode】273. Integer to English Words

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

  4. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

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

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

  6. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

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

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

  8. LeetCode 273. Integer to English Words

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

  9. Integer to English words leetcode java

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

随机推荐

  1. 清除 WD MyCloud 自动生成的 .wdmc 目录

    1. 先 SSH,停止相应服务 /etc/init.d/wdmcserverd stop/etc/init.d/wdphotodbmergerd stop 2. 禁止服务自启动 update-rc.d ...

  2. iOS Json转换模型库:YYModel

    iOS Json转换模型库:YYModel   其实在研究这个库之前,市面上已经有很多类似的模型序列化成JSON及反序列化库(如Mantle.MJExtension)了,推荐他只是因为他高端的性能和容 ...

  3. hdu 猜数字

    这题的意思是找到最大的n使得m次之内的猜测可以猜到1~n之间的任何值.这里是二分思想的逆过程,1~h个数最多猜测log2(n+1)次(n为奇数),故 n=2^m-1; #include"io ...

  4. Linux邮件服务器架构

    // 上面的过程只是实现了简单的本地用户的文件发送功能,只需要安装mailutil,不需要安装配置sendmail,看鸟哥的Linux私房菜中写的应该是本地用户发送邮件不需要sendmail.只有当非 ...

  5. 移动Web应用开发入门指南——兼容篇

    兼容篇 兼容篇是我最想写的一部分,在这之前也总结过很多关于移动开发的兼容问题与解决方案.对于移动Web开发来说,兼容是开发重心,通常要花费30%甚至更多的时间去处理一些兼容问题,甚至时间花掉了,问题依 ...

  6. 2016.07.08,英语,《Vocabulary Builder》Unit 24

    mand/mend comes from mandare, Latin for 'entrust' or 'order'. command and commandment: [kə'mændmənt] ...

  7. 如何使用Xcode分析调试在真机运行的UE4 IOS版游戏

    写本文的是因为UE4 官方文档虽然也有,但主要讲的是是用UE4Editor把游戏打成一个IPA包的形式发布的方法 而对于想通过Xcode分析UE4的渲染流程来学习或优化的朋友,那官方文档的资料还是不够 ...

  8. 解决ORA-00054资源正忙的问题

    有时候在drop表或者其他对象的时候,会遇到ORA-00054:资源正忙,要求指定NOWAIT(中文字符集)或者ORA-00054: resource busy and acquire with NO ...

  9. 20145317彭垚 java课程总结

    课程总结 每周读书笔记链接汇总 第一周http://www.cnblogs.com/5317p/p/5248481.html 第二周http://www.cnblogs.com/5317p/p/527 ...

  10. Aliasing 走样

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Have you ever noticed the weird &quo ...