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. MQTT再学习 -- 搭建MQTT服务器及测试

    最近在搞 PM2.5 采集,需要用到 MQTT 传输协议.协议部分看了几天的,讲的七七八八.本身在 intel 上有 写好的MQTT 的源码,现在的工作其实也就是移植到单片机上或者DM368板卡上.不 ...

  2. bacnet ip转MQTT

    迈思德网关最新支持BACNET IP协议,可以将BACNET IP转换为MODBUS.MQTT.OPC等协议,与百度天工,阿里云等无缝对接. 支持AI.AO.DI.DO.AV.DV六个对象的读写.

  3. tf.device()指定tensorflow运行的GPU或CPU设备

    在tensorflow中,我们可以使用 tf.device() 指定模型运行的具体设备,可以指定运行在GPU还是CUP上,以及哪块GPU上. 设置使用GPU 使用 tf.device('/gpu:1' ...

  4. 专业软件 —— Adobe Audition

    0. 中英文对照 mute:静音,solo:独奏,arm to record:准备录音: reverb:混响: 1. 简介 Adobe Audition CS6原身为经典的音频后期处理软件Cool E ...

  5. CUDA 安装完成以后如何判断安装是否成功

    最近在家里过寒假,可能这是还在学校里带着最大的福利了,无意之中翻出了多年前买的几本关于CUDA编程的书,于是随便在自己电脑上配置了一下环境,试试能不能把当年没有看完的书给看完了,于是有了今天这个判断C ...

  6. latex的使用

    要写论文了,要用到latex,总算明白了一些,在ubuntu下安装好texlive和texmaker后,在终端测试,输入命令tex: 出现上面的图说明安装好了. 在texmaker下编写tex文档,保 ...

  7. ubuntu下的wps office for linux

    今天修改老师给的论文,也没备份,悲剧的一幕发生了,原来用的word在ubuntu里打开,结果图片显示不出来,也不知怎么修复,哭死... 网上搜索答案,一位热心的网页推荐我用wps office for ...

  8. BZOJ1835: [ZJOI2010]base 基站选址【线段树优化DP】

    Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄 ...

  9. MyBatis_Study_004(动态代理)

    源码:https://github.com/carryLess/mbtsstd-004 0.readme 基于前几篇:dao的实现类基本煤气到什么作用 仅仅是通过SQLSession的相应API定位到 ...

  10. hadoop之 hadoop日志存放路径

    环境:[root@hadp-master hadoop-2.7.4]# hadoop versionHadoop 2.7.4 Hadoop的日志大致可以分为两类: (1).Hadoop系统服务输出的日 ...