原题链接在这里:https://leetcode.com/problems/integer-to-english-words/

题目:

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"

Example 3:

Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Example 4:

Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

题解:

没三个digit分成一个 unit, 用unitNumber 函数把这三位数换算成数字加上对应的unit.

Note: num = 1,000,000时不可以出现 "One Million Thousand"的情况,所以while循环时 要加上if(rest>0)的限定条件.

Time Complexity: O(n), n是unit的个数 Space: O(1).

AC Java:

 public class Solution {
public String numberToWords(int num) {
if(num == 0){
return "Zero";
}
String res = "";
String [] units = {"", " Thousand", " Million", " Billion"};
int i = 0;
while(num > 0){
int unit = num%1000;
if(unit > 0){ //error 1,000,000
res = unitNumber(unit) + units[i] + (res.length() == 0 ? "" : " "+res);
}
num = num/1000;
i++;
}
return res;
} private String unitNumber(int unit){
String res = "";
String [] ten = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String [] twenty ={"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
String [] hundred = {"Zero","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; int a = unit/100;
int b = unit%100;
int c = unit%10; if(a > 0){
res = ten[a] + " Hundred";
} if(b>=10 && b<20){
if(res.length() != 0){
res = res + " ";
}
res = res + twenty[b%10];
return res;
}else if(b >= 20){
if(res.length() != 0){
res = res + " ";
}
b = b/10;
res = res + hundred[b];
} if(c > 0){
if(res.length() != 0){
res = res + " ";
}
res = res + ten[c];
}
return res;
}
}

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

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

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

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

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

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

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

  5. 【LeetCode】273. Integer to English Words

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

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

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

  7. 273. Integer to English Words

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

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

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

  9. [LC] 273. Integer to English Words

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

随机推荐

  1. java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子

    [学习笔记] 6.Oracle DataSource and SingleTon: import oracle.jdbc.pool.OracleDataSource;import java.sql.C ...

  2. C++ 用 new 生成一个动态二维数组

    //Microsoft Visual Studio 2015 Enterprise //变长二维数组 #include <iostream> #include<iomanip> ...

  3. Ubuntu bashrc profile environment 区别

    在 Ubuntu 中有如下几个文件可以设置环境变量: /etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. ...

  4. leetcode 2019.10.29 首次破百

    刷题首次破百,记录一下自己成长的历程. 仍在路上,会慢慢变强的~

  5. nmap使用帮助翻译

    Nmap 7.60 ( https://nmap.org )Usage: nmap [扫描类型] [操作] {目标说明}目标说明:  可以识别主机名.IP地址.网络,等等.  例如: scanme.n ...

  6. netty--buffer分配策略

    AdaptiveRecvByteBufAllocator 动态分配buffer大小的类. 如果前一次读取完全填满了分配的缓冲区,它将逐渐增加预期的可读字节数.(增加的方式:初始化类的时候,会预先设置好 ...

  7. maven配置阿里镜像

    在conf\settings.xml 在<mirrors>里面添加   <mirror>    <id>nexus-aliyun</id>    < ...

  8. Spring Cloud Alibaba学习笔记(9) - RocketMQ安装与RocketMQ控制台

    搭建RocketMQ 系统环境准备 64位操作系统,推荐使用Linux.Unix.MacOS 64位 JDK1.8+ Maven 3.2.x 适用于Broker服务器的4g +可用磁盘 下载与搭建 下 ...

  9. Powershell学习笔记:(二)、基础知识

    从Window7以后,WIndows系统都自带了Windows PowerShell. 自带版本如下 WIndow7  2.0 WIndow8   3.0 Window8.1      4.0 Win ...

  10. element 右键菜单

    右键菜单 这是一个右键菜单的组件 <template> <div id="dropMenu" :style="style" style=&qu ...