LeetCode 273. Integer to English Words
原题链接在这里: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的更多相关文章
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- leetcode-【hard】273. Integer to English Words
题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...
- 【LeetCode】273. Integer to English Words
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- 【LeetCode】Integer to English Words 解题报告
Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...
- 273. Integer to English Words
题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...
- 273. Integer to English Words数字转为单词
[抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
- [LC] 273. Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
随机推荐
- WiFi、ZigBee、BLE用哪个?
小米是这么选的: 1) 插电的设备,用WiFi: 2) 需要和手机交互的,用BLE: 3) 传感器用ZigBee. WIFI,WIFI是目前应用最广泛的无线通信技术,传输距离在100-300M,速率可 ...
- java当中请给出一个oracle的helloworld例子
[学习笔记] 2.oracle的helloworld例子: import java.sql.*;public class OracleHello{ public static void main ...
- C之指针加减运算
法则:1.指针减指针,语法正确,结果得一个整型值,表示两数值之间的对象类型的空间距离,而不是对象之间的字节数差值 2.指针加指针,语法错误, 3.指针加整形值,语法正确,表示后移N个空间单位 ...
- Scratch编程:躲开鲨鱼(五)
“ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 这是一款简单的小游戏,实现了用鼠标控制一条小海星在水里游 ...
- PB之常用函数
原文网址:https://www.cnblogs.com/zhaoxiong/p/8082523.html PB之常用函数 弹出窗口:messagebox() 基本写法:Messagebox('标题' ...
- Java Embeded 包 与各个架构之间的关系
Oracle Java Embedded Suite 7.0 for Linux x86 V37917-01.zip Oracle Java Embedded Suite ...
- kafka的安装及使用(单节点)
介绍了linux环境下,kafka 服务的安装与配置 安装 jdk 环境 下载 kafka 源码包放到服务器,解压 开启 zookeeper 开启 kafka server 创建主题 开启生产者 开启 ...
- Mongo常用查询语法
一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...
- JS 页面刷新以及页面返回的几种方式
1.通过标签形式的跳转页面 <a class="popup" href="~/WeiXin/Shoppingguide/StockData">&l ...
- UI5-技术篇-Hybrid App-1-Barcode扫描
参考资料: https://www.w3cschool.cn/cordova/cordova_overview.html https://blogs.sap.com/2017/01/03/sapui5 ...