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. Java读取txt文件信息并操作。

    一.java读取txt文件内容 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Fi ...

  2. C#中时间计算方法汇总

    这几天要做一个关于时间数据统计的页面,发现有些东西还是比较用的,现总结如下. DateTime dt = DateTime.Now;  //当前时间 DateTime startWeek = dt.A ...

  3. POI使用 (4.0) 常用改动

    POI 升级到高版本后,原有的EXCLE导入导出工具类部分代码已不适用,目前只是对我自己写的工具类的过期代码进行更新,以后继续更新 若有问题请指出,再修改 1.数据类型 Cell.CELL_TYPE_ ...

  4. python3 openpyxl基本操作

    #coding:utf-8 import xlrd import xlwt # 读写2007 excel import openpyxl import sys #读取设备sn # def readSN ...

  5. ballerina 学习二十五 项目docker 部署&& 运行

    ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...

  6. macOS -- Mac系统如何通过终端使用mysql

    打开终端,输入下面的命令 mysql -u root -p 如果提示输入密码,并且能直接进入,那就太棒了,下面的就不用看了,直接使用就好了 如果没有这么幸运,提示 command not found ...

  7. CentOS 6.5下安装Oracle 11g(转)

    最近开始学习CentOS使用,做些记录. 参考文献:Cent OS 6_5(x86_64)下安装Oracle 11g 一.硬件要求 1.内存&swap Minimum:1 GB of RAM ...

  8. 修改numa和io调度优化mysql性能

    一.NUMA设置单机单实例,建议关闭NUMA,关闭的方法有三种:1.硬件层,在BIOS中设置关闭:2.OS内核,启动时设置numa=off:3.可以用numactl命令将内存分配策略修改为interl ...

  9. X.509证书的编码及解析:程序解析以及winhex模板解析

    一.证书的整体结构:证书内容.签名算法.签名结果. 用ASN.1语法描述如下: Certificate::=SEQUENCE{ tbsCertificate TBSCertificate, signa ...

  10. java scanner工具类

    import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanne ...