Integer to English Words 解答
Question
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
For example,
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
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)
Solution
根据英文表示数字的规则,这题的关键在于三位三位的处理数字。
小于100的数:1. 小于20的,没有什么规律,只能存到数组里 2. 大于等于20的,可以按十位和个位拆解。
如 23,345,100,089
先和billion(1,000,000,000)比较,除法得到23
再和million(1,000,000)比较,除法得到345,345再和hundred(100)比较,得到3和45
。。。
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0:
return "Zero"
less_then_twenty_words = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
ty_words = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
dex_words = ["Billion", "Million", "Thousand", "Hundred"]
radix = [1000000000, 1000000, 1000, 100]
s = ""
for i in range(len(radix)):
count = num / radix[i]
if count == 0:
continue
s = s + self.numberToWords(count) + " " + dex_words[i] + " "
num = num % radix[i]
if num < 20:
s = s + less_then_twenty_words[num]
elif num < 100:
s = s + ty_words[num / 10 - 2] + " " + less_then_twenty_words[num % 10]
return s.strip()
Similar way, but more easy understanding codes.
public class Solution {
private Map<Integer, String> map = new HashMap<>(); public String numberToWords(int num) {
fillMap();
if (num == 0) {
return map.get(0);
}
List<String> list = new ArrayList<>();
StringBuilder sb = new StringBuilder();
if (num >= 1000000000) {
int extra = num / 1000000000;
list.addAll(convert(extra));
list.add("Billion");
num = num % 1000000000;
} if (num >= 1000000) {
int extra = num / 1000000;
list.addAll(convert(extra));
list.add("Million");
num = num % 1000000;
} if (num >= 1000) {
int extra = num / 1000;
list.addAll(convert(extra));
list.add("Thousand");
num = num % 1000;
} if (num >= 100) {
int extra = num / 100;
list.addAll(convert(extra));
list.add("Hundred");
num = num % 100;
} if (num > 0) {
list.addAll(convert(num));
} for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i) + " ");
} return sb.toString().trim();
} private List<String> convert(int x) {
List<String> list = new ArrayList<>();
if (x >= 100) {
int num = x / 100;
list.add(map.get(num));
list.add("Hundred");
x = x % 100;
} if (x > 0) {
if (x > 20) {
int remainder = x % 10;
x = x - remainder;
list.add(map.get(x));
if (remainder > 0) {
list.add(map.get(remainder));
}
} else {
list.add(map.get(x));
}
}
return list;
} private void fillMap() {
map.put(0, "Zero");
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
map.put(4, "Four");
map.put(5, "Five");
map.put(6, "Six");
map.put(7, "Seven");
map.put(8, "Eight");
map.put(9, "Nine");
map.put(10, "Ten");
map.put(11, "Eleven");
map.put(12, "Twelve");
map.put(13, "Thirteen");
map.put(14, "Fourteen");
map.put(15, "Fifteen");
map.put(16, "Sixteen");
map.put(17, "Seventeen");
map.put(18, "Eighteen");
map.put(19, "Nineteen");
map.put(20, "Twenty");
map.put(30, "Thirty");
map.put(40, "Forty");
map.put(50, "Fifty");
map.put(60, "Sixty");
map.put(70, "Seventy");
map.put(80, "Eighty");
map.put(90, "Ninety");
}
}
Integer to English Words 解答的更多相关文章
- 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 ...
- [LeetCode] 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
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to it ...
- 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 (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [Swift]LeetCode273. 整数转换英文表示 | Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- Integer to English words leetcode java
问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
随机推荐
- Ugly Number II 解答
Question Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime ...
- kafka-分布式消息系统
消息中间件MessageQuene 解耦且可扩展:业务复杂度的提升带来的也是耦合度的提高,消息队列在处理过程中间插入了一个隐含的.基于数据的接口层,两边的处理过程都要实现这一接口.这允许你独立的扩展或 ...
- Windows下模拟Linux开发
1.背景 Linux环境下开发是大势所趋,也是开发者必须掌握的技能.然windows系统已深入人心,实在不想放弃windows下的成熟应用,因此可以在Windows上模拟一个Linux系统.这样就满足 ...
- Android项目中gen文件下R文件无法生成的解决的方法
帮一个网友解决R文件无法生成的问题,搜集了些材料特整理例如以下,刚開始学习的人參考他人代码时极易出现此种问题,一般都是xml文件出错,无法被正确解析. gen文件夹无法更新,或者gen文件夹下的R.J ...
- 搭建完全分布式的hadoop[转]
hadoop 创建用户及hdfs权限,hdfs操作等常用shell命令 sudo addgroup hadoop#添加一个hadoop组sudo usermod -a -G hadoop larry# ...
- 浅谈GitLab与Git
前言:先解释下关于库的认识. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新增项目(远程仓库) 在GitLa ...
- 网站飘窗js代码
<SCRIPT> var imagepath="/${res}/images/geren.jpg" ; var imagewidth=178 ;//这两行写图片的大小 ...
- IOS — 关于Socket传输文件需要自定义延时或者包大小的情况
1. 首先导入头文件 #include <stdio.h> #include <errno.h> #include <string.h> #include < ...
- jQuery选择器的学习
jQuery的核心在于它的选择器,通过观看视频和阅读,发现jQuery选择器大体上的分类可分为这么几种(不同人方式不同,这里选择一个自认为比较好的): 1.基础选择器(对应api文档中的基本选择器和层 ...
- linux修改密码
情景:Linux 服务器上用户的密码被服务器管理员发现太过简单,需要重置密码.处理时为了方便记忆,就直接使用普通用户登录,修改密码时,在原密码的基础上增加一串特定的数字,结果提示不通过.例如出现错误提 ...