题目:

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. 20145129 《Java程序设计》第1周学习总结

    20145129 <Java程序设计>第1周学习总结 教材学习内容总结 在第一章学习后初步了解了Java历史及发展,以及JCP,JSR,JVM的相关知识了解.JCP是一个开放性国际组织,由 ...

  2. 软件工程随堂小作业——(C++)

    一.设计思路 本来我的思路是根据上楼的人数和上楼的层数来计算出平均值,但是我发现这个思路不对.于是我选择了最笨的方法,复杂度为O(n2). (1)输入坐电梯的人数和要去的楼层: (2)找到输入楼层里最 ...

  3. 安装配置OPENCMS的Replication cluster(从)详细过程

    1.  把opencms.war拷贝到tomcat下的webapps目录,启动tomcat服务. 2.  在安装之前,打开解压缩后的war包目录(tomcat启动后会自动把war包解开),删除目录 $ ...

  4. Chapter 3 Discovering Classes and Object

    Chatper 3 Discovering Classes and Object Exercises: 1.What is a class? A class is a template for man ...

  5. Netsharp快速入门(之13) 销售管理(单据流转 销售订单生成发货单)

    作者:秋时 杨昶   转载须说明出处 4.5     单据流转 4.5.1  单据流转的目的 单据流转主要为了实现业务关系的流转,并记录相互之间的关系.例如从销售订单生成销货单,两张单据之间有对应的关 ...

  6. SpringMVC:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax;

    今天用SpringMVC做修改添加操作,之前的操作都实现了添加修改,但始终报com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have ...

  7. ```````````````辐射度 Radiometry

    solid angel --立体角 单位 sr  球面度 dω就是对solid angel的微分 4π代表一个球 我发现dω就是对半径为1的球的表面积的微分 所以4π代表一个球  这就是球的表面积.. ...

  8. .NET设计模式(19):观察者模式(Observer Pattern)(转)

    概述 在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系” ——一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知.如果这样的依赖关系过于紧密,将使软件不能很好地抵御 ...

  9. CSS3 transition规范的实际使用经验

    本篇文章主要讲述CSS3 transition规范和在不同浏览器之间的使用差异,关于具体解决方法或如何规避问题的意见可以参考另一篇非常有见地的文章,“All You Need to Know Abou ...

  10. LCIS(m*n) 最长公共上升子序列

    详见:http://wenku.baidu.com/view/3e78f223aaea998fcc220ea0n3的: for(int i=1;i<=n;i++)             for ...