273. 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"
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
个位数是0的时候不用说zero,直接说英文名就行了
所以<10数组的第一位是“”
[思维问题]:
不知道怎么把数字分开,/ k % k就行了,但是这道题的重点并不是把数分开,而是如何处理10、20、100内有英文名的特殊数字以及hundred thousand million billion
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
“英文名” + 每次都新建字符串的helper函数连接一下就行了
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
可能出现空格字符串啊,所以都先赋值给result, 最后再统一string.trim一下
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
记住10、20、100内有英文名的特殊数字以及hundred thousand million billion 才有英文名
helper函数中每次都新建字符串,才能和一般字符串用+连接
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
public class Solution {
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];
else if (num < 100) result = belowHundred[num/10] + " " + helper(num % 10);
else if (num < 1000) result = helper(num/100) + " Hundred " + helper(num % 100);
else if (num < 1000000) result = helper(num/1000) + " Thousand " + helper(num % 1000);
else if (num < 1000000000) result = helper(num/1000000) + " Million " + helper(num % 1000000);
else result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
return result.trim();
}
}
273. Integer to English Words数字转为单词的更多相关文章
- [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] 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
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- 273. Integer to English Words
题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...
- [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
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/description/ 题目: Convert a non-negati ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [LC] 273. Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
随机推荐
- i.e 和e.g 的区别
i.e 和e.g 的区别 两者都是拉丁文缩写 i.e是id est的缩写,意思是that is. e.g是exempli gration的缩写,意思是for example;
- Maven使用中的常见问题整理
1.更新eclipse的classpath加入新依赖 1.在dependencyManagement里面加入包括版本在内的依赖信息,如: <dependency> <groupId ...
- 自己定义一个tab指令
定义一个tab切换的指令: 指令的文件结构: Js/directives/tab tab.html tab.js tab.html: <style> .my-li-style{ line- ...
- Intellij解决版本冲突时,merge界面各区块颜色区分
灰色:删除 蓝色:改变 绿色:新增 红色:冲突
- 【BZOJ】2938 [POI2000]病毒(AC自动机)
题目 传送门:QWQ 传送到洛谷QWQ 分析 夏爷爷传送门 代码 #include <bits/stdc++.h> using namespace std; ; ][], fail[N*] ...
- Gson:自定义TypeAdapter
当前项目解析json用的工具是google的gson,原因嘛,因为有GsonFormat插件,可以直接把服务端传回的json字符串转成Bean对象.不过在实际使用中出现了以下两个问题: 传回的字符串或 ...
- OSPF理论介绍
OSPF协议Open Shortest Path First是链路状态协议.RIP和EIGRP是距离矢量协议只知道邻居,但链路状态可知道全网信息,因此天然免疫环路.距离矢量协议和链路状态协议最大的区别 ...
- 自然语言处理NLTK
Python文本分析工具NLTK 情感分析 文本相似度 文本分类 分类预测模型:朴素贝叶斯 实战案例:微博情感分析
- congst与指针
指向const的指针 //a pointer to const int;指针指向常量对象,相对本指针而言,不能指针指向的对象的常量,不能通过本指针修改常量对象指针,实际的对象不一定的常量 const指 ...
- maven项目将web2.5改为web3.1
用maven构建的web项目默认的web.xml为2.3的版本,而我们需要更改为我们想要的版本(3.1). 在这里有两种方式更改web.xml的版本: 第一种: 将项目切换为navigator视图,然 ...