题目:

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"

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

链接: http://leetcode.com/problems/integer-to-english-words/

题解:

把数字翻译成英文。这个跟Integer to Roman很像,把情况分清楚就不难解决。代码大都参考了Discuss里hwy_2015的,简洁易懂。主要是以1000为一个单位来把数组分成组,每个组内单独处理tens和lessThanTwenty的情况。

Time Complexity - O(n), Space Complexity - O(n)。

public class Solution {
private final String[] lessThanTwenty = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private final String[] thousands = {"" ,"Thousand", "Million", "Billion"}; public String numberToWords(int num) {
if(num <= 0) {
return "Zero";
}
String words = "";
int index = 0; while(num > 0) {
if(num % 1000 != 0) {
words = getNum(num % 1000) + thousands[index] + " " + words;
}
num /= 1000;
index++;
} return words.trim();
} private String getNum(int num) {
if(num <= 0) {
return "";
} else if (num < 20) {
return lessThanTwenty[num] + " ";
} else if (num < 100) {
return tens[num / 10] + " " + getNum(num % 10);
} else {
return lessThanTwenty[num / 100] + " Hundred " + getNum(num % 100);
}
}
}

Reference:

https://leetcode.com/discuss/55462/my-clean-java-solution-very-easy-to-understand

https://leetcode.com/discuss/55349/if-you-know-how-to-read-numbers-you-can-make-it

https://leetcode.com/discuss/71544/short-clean-java-solution

https://leetcode.com/discuss/60010/share-my-clean-java-solution

https://leetcode.com/discuss/55273/my-java-solution

https://leetcode.com/discuss/55477/recursive-python

273. Integer to English Words的更多相关文章

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

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

  2. 【LeetCode】273. Integer to English Words

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

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

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

  4. [leetcode]273. Integer to English Words 整数转英文单词

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

  5. 273. Integer to English Words数字转为单词

    [抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...

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

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

  7. LeetCode 273. Integer to English Words

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

  8. [LC] 273. Integer to English Words

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

  9. 273 Integer to English Words 整数转换英文表示

    将非负整数转换为其对应的英文表示,给定的输入是保证小于 231 - 1 的.示例:123 -> "One Hundred Twenty Three"12345 -> & ...

随机推荐

  1. [转载]求平方根sqrt()函数的底层算法效率问题

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

  2. C++中的set和java的hashset有何区别?

    以前对C++的STL容器烂熟于心,两年没碰过C++了,现在已经很生疏了.工作原因转战java,对java的容器不甚了解,特别是每看到一种容器,不由自主地拿起和C++对比.C++中的set和java的h ...

  3. “我爱淘”第二冲刺阶段Scrum站立会议2

    完成任务: 对发布页面优化了一下,并将登陆的功能实现了一点,就是还没有实现注册的功能 . 计划任务: 在客户端实现分类功能,通过学院的分类查看书籍. 遇到问题: 自动将数据库的内容返回到客户端.

  4. HTML统一资源定位器

    w3c 更好的解释 在OSGi in action中安装bundle时要加文本传输协议,要不然shell判断不出来

  5. .NET Framework 4.5、4.5.1 和 4.5.2 中的新增功能

    .NET Framework 4.5.4.5.1 和 4.5.2 中的新增功能 https://msdn.microsoft.com/zh-cn/library/ms171868.aspx

  6. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  7. MyEclipse: Can't load IA 32-bit .dll on a AMD 64-bit platform

    java.lang.UnsatisfiedLinkError: D:\Tomcat7\apache-tomcat-7.0.59\bin\tcnative-1.dll: Can't load IA 32 ...

  8. FastDFS配置文件(storage.conf)

    # 该配置文件是否生效 # false:生效 # true:无效 disabled=false # 本storage server所属组名 group_name=group1 # 绑定IP # 后面为 ...

  9. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  10. Feature Engineering versus Feature Extraction: Game On!

    Feature Engineering versus Feature Extraction: Game On! "Feature engineering" is a fancy t ...