[LeetCode] 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"
Example 4:
Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety
新版题目没有提示了。
Hint:
- Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
- Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
- There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
将一个整数转换成英文单词。提示:要3个一组进行处理,限定了数字范围为0到2^31 - 1之间,最高只能到billion位,只需处理四组。
每三位一组进行处理,每加一组就加上units,整数除以1000,对1000取余。处理范围分为99以上,20~99,10~20,0~10。先写出不同的数字范围英文单词列表,然后根据范围添加到结果中。
F家:可能是负数
Java:
class Solution {
public String numberToWords(int num) {
String[] units = {""," Thousand"," Million"," Billion"};
int i = 0;
String res="";
while(num > 0) {
int temp = num % 1000;
if(temp > 0) res = convert(temp) + units[i] + (res.length()==0 ?"": " "+res);
num /= 1000;
i++;
}
return res.isEmpty()? "Zero" : res;
}
public String convert(int num){
String res = "";
String[] ten = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
String[] hundred = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
String[] twenty = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
if(num>0) {
int temp = num / 100;
if(temp > 0) {
res += ten[temp] + " Hundred";
}
temp = num%100;
if(temp >= 10 && temp < 20){
if(!res.isEmpty()) res +=" ";
res = res + twenty[temp%10];
return res;
}else if(temp >= 20){
temp = temp / 10;
if(!res.isEmpty()) res +=" ";
res = res + hundred[temp-1];
}
temp = num % 10;
if(temp > 0) {
if(!res.isEmpty()) res +=" ";
res = res + ten[temp];
}
}
return res;
}
}
}
Python:
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0:
return "Zero" lookup = {0: "Zero", 1:"One", 2: "Two", 3: "Three", 4: "Four", \
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", \
10: "Ten", 11: "Eleven", 12: "Twelve", 13: "Thirteen", 14: "Fourteen", \
15: "Fifteen", 16: "Sixteen", 17: "Seventeen", 18: "Eighteen", 19: "Nineteen", \
20: "Twenty", 30: "Thirty", 40: "Forty", 50: "Fifty", 60: "Sixty", \
70: "Seventy", 80: "Eighty", 90: "Ninety"}
unit = ["", "Thousand", "Million", "Billion"] res, i = [], 0
while num:
cur = num % 1000
if num % 1000:
res.append(self.threeDigits(cur, lookup, unit[i]))
num //= 1000
i += 1
return " ".join(res[::-1]) def threeDigits(self, num, lookup, unit):
res = []
if num / 100:
res = [lookup[num / 100] + " " + "Hundred"]
if num % 100:
res.append(self.twoDigits(num % 100, lookup))
if unit != "":
res.append(unit)
return " ".join(res) def twoDigits(self, num, lookup):
if num in lookup:
return lookup[num]
return lookup[(num / 10) * 10] + " " + lookup[num % 10]
Python:
class Solution(object):
def numberToWords(self, num):
lv1 = "Zero One Two Three Four Five Six Seven Eight Nine Ten \
Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen".split()
lv2 = "Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety".split()
lv3 = "Hundred"
lv4 = "Thousand Million Billion".split()
words, digits = [], 0
while num:
token, num = num % 1000, num / 1000
word = ''
if token > 99:
word += lv1[token / 100] + ' ' + lv3 + ' '
token %= 100
if token > 19:
word += lv2[token / 10 - 2] + ' '
token %= 10
if token > 0:
word += lv1[token] + ' '
word = word.strip()
if word:
word += ' ' + lv4[digits - 1] if digits else ''
words += word,
digits += 1
return ' '.join(words[::-1]) or 'Zero'
C++:
class Solution {
public:
string numberToWords(int num) {
string res = convertHundred(num % 1000);
vector<string> v = {"Thousand", "Million", "Billion"};
for (int i = 0; i < 3; ++i) {
num /= 1000;
res = num % 1000 ? convertHundred(num % 1000) + " " + v[i] + " " + res : res;
}
while (res.back() == ' ') res.pop_back();
return res.empty() ? "Zero" : res;
}
string convertHundred(int num) {
vector<string> v1 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
vector<string> v2 = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string res;
int a = num / 100, b = num % 100, c = num % 10;
res = b < 20 ? v1[b] : v2[b / 10] + (c ? " " + v1[c] : "");
if (a > 0) res = v1[a] + " Hundred" + (b ? " " + res : "");
return res;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 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] 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 (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- LeetCode 273. Integer to English Words
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/description/ 题目: Convert a non-negati ...
- 273. Integer to English Words数字转为单词
[抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
- 273 Integer to English Words 整数转换英文表示
将非负整数转换为其对应的英文表示,给定的输入是保证小于 231 - 1 的.示例:123 -> "One Hundred Twenty Three"12345 -> & ...
- 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 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
随机推荐
- pycharm下site-packages文件标记为红的问题;pycharm无法识别本地site-packages问题
当图示红框标记区域的文件夹颜色显示红色时,需要到FIle-setting里面设置好本地的运行环境,设置错误就会导致引用问题: 启动谷歌浏览器 from selenium import webdrive ...
- 微信小程序~跳页传参
[1]需求: 点击商品,跳到相应商品详情页面 [2]代码: (1)商品列表页 <view class="goodsList"> <view wx:for=&quo ...
- 使用gitlab下载代码(附常用命令)
Git是现在很多人常用的代码管理工具,这里有一些常用的命令详解,本人接触也不是很久,若有错误,请在评论指出,谢谢. 若计算机中没有安装GIT,可自行查找安装教程,十分简便. ①首先,我们需要下载项目, ...
- 微信支付之获取openid
一.准备工具 不管开发什么,官方的文档应该是第一个想到的这里把官方文档贴出来:微信网页授权文档除此之外,我们还需要一个内网穿透的工具在开发环境下让微信能访问到我们的域名.我使用的是natapp.此类工 ...
- c# 3.0语言主要增强
1隐含类型的局部变量 var i=5; var h=23.56; var s="Cshap" var intarr=new[]{1,2,3}; var 为关键字,可以根据后边的初始 ...
- django-mysql事务
django文档:https://yiyibooks.cn/xx/django_182/topics/db/transactions.html mysql事务 1) 事务概念 一组mysql语句,要么 ...
- 五.python小数据池,代码块的最详细、深入剖析
一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了,什么是内存地址呢? 你只要创建一个数据(对象)那么都会在内存中开辟一个空间,将这个数据临时加在到内存中,那么这个空间是 ...
- spingboot jar 包启动遇到得坑
先摘抄一篇文章 pringboot打成jar包后,可直接用java -jar app.jar 启动,或者使用 nohup java -jar app.jar & 后台启动,也可以将 jar包链 ...
- Time Frequency (T-F) Masking Technique
时频掩蔽技术. 掩蔽效应 声掩蔽(auditory masking)是指一个声音的听阈因另一个声音的存在而上升的现象.纯音被白噪声所掩蔽时,纯音听阈上升的分贝数,主要决定于以纯音频率为中心一个窄带噪声 ...
- P4848 崂山白花蛇草水
题意:支持修改的矩形第 \(k\) 大. 题解:动态开点权值线段树 套 Kd-tree. 然后也没什么难的但就是写不对...调了两天才调出来然后发现跑的巨慢,于是又%了一发Claris'题解,跑的真快 ...