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"

题目:

给定一个整数,像check一样将该数字用英文读出来

思路:

1. Coz given input is less than 231 - 1, we can say that the highest digit level is billion

2. We divide input into N parts, all the parts follow the similar partten of converting.  We use helper function to help recursive call.

代码:

 class Solution {
// coz array index begins at 0, so we use "" to empty a space
private final String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private final String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] belowHundred = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; public String numberToWords(int num) {
if (num == 0) return "Zero";
return helper(num);
} private String helper(int num) {
String result = new String();
if (num < 10) result = belowTen[num];
else if (num < 20) result = belowTwenty[num -10];
// if num is smaller than 100, keep tens digit and add units digit
else if (num < 100) result = belowHundred[num/10] + " " + helper(num % 10);
// if num is smaller than thousand, call helper to get how many hundreds, call the helper to get the rest digits
else if (num < 1000) result = helper(num/100) + " Hundred " + helper(num % 100);
// if num is smaller than million, call helper to get how many thousands, call the helper to get the rest digits
else if (num < 1000000) result = helper(num/1000) + " Thousand " + helper(num % 1000);
// if num is smaller than billion, call helper to get how many millions, call the helper to get the rest digits
else if (num < 1000000000) result = helper(num/1000000) + " Million " + helper(num % 1000000);
else result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
return result.trim();
}
}

[leetcode]273. Integer to English Words 整数转英文单词的更多相关文章

  1. [LeetCode] 273. 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 整数转为英文单词

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

  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

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

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

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

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

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

  7. 【LeetCode】273. Integer to English Words

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

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

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

  9. 273. Integer to English Words

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

随机推荐

  1. 5.4完成其他模块的xadmin后台注册

    courses  adminx.py from .models import Course, Lesson, Video, CourseResource import xadmin class Cou ...

  2. PyAlgoTrade Hello World 第一个程序(一)

    本教程的目标是快速介绍PyAlgoTrade.PyAlgoTrade的目标是帮助您实现股票交易策略.假设您有一个交易策略的想法,并且您希望使用历史数据进行评估,并查看其行为方式,那么PyAlgoTra ...

  3. 自定义redis session

    1.思路 2.程序实现 1.用户系统类 这里模拟一个蹩脚的用户系统类(userSystem),如下: #coding=utf-8 #Redis实现用户系统 __author__ = 'beginman ...

  4. streamsets docker 安装试用

    docker 安装 docker run --restart on-failure -p 18630:18630 -d --name streamsets-dc streamsets/datacoll ...

  5. Nginx 服务器伪静态配置不当造成 Access denied

    Nginx 服务器伪静态配置不当造成 Access denied 有群有反馈将 FastAdmin 布署到阿里云后无法打开后台. 出现如下提示,首页是可以打开,点登录链接后出现的.(下是群友的截图) ...

  6. VS2010 C++环境下DLL和LIB文件的生成与调试 备忘

    利用VS2010工具,调试DLL文件的方法现总结如下: 在一个解决方案中生成两个工程,假设MYDLL和MYDLG两个工程,前者是DLL工程,后者DLG调用前边的DLL工程.设置如下: 目录如下:图,本 ...

  7. Excel VBA 找出选定范围不重复值和重复值

    Sub 找出选定范围内不重复的值() On Error Resume Next Dim d As Object Set d = CreateObject("scripting.diction ...

  8. RecordingOptions录制设置选项

    1.录制思考时间 2.录制方式 3.自定义证书 4.非资源选项

  9. 生成Excel

    生成Excel 需要引用MyXls.SL2.dll的类库: 下载地址:http://sourceforge.net/projects/myxls/ 命名空间using org.in2bits.MyXl ...

  10. Synergy使用(安装及配置)

    最近在看一篇文章,找到了一款符合我需求的软件:Synergy. Synergy可以在多台电脑上进行鼠标与键盘及剪贴板(只能共享文本)的共享.不用每台电脑都插上这些外设了... 共享文件可以参看微软出品 ...