[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[] = {"", ...
随机推荐
- 《Java程序设计实验》 软件工程18-1,3 OO实验2
- kafka题目
1. Kafka的用途有哪些?使用场景如何?2. Kafka中的ISR.AR又代表什么?ISR的伸缩又指什么3. Kafka中的HW.LEO.LSO.LW等分别代表什么?4. Kafka中是怎么体现消 ...
- CentOS7.5安装SVN和可视化管理工具iF.SVNAdmin
一.安装Apache和PHP 由于iF.SVNAdmin使用php写的,因此我们需要安装php yum install httpd php 二.安装SVN服务器(其中,mod_dav_svn是Apac ...
- 《BUG创造队》第三次作业:团队项目原型设计与开发
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验六 团队作业3:团队项目原型设计与开发 团队名称 BUG创造队 作业学习目标 ①掌握软件原型开发技术:②学会使用软件原型 ...
- Zookeeper基础入门介绍
什么Zookeeper Zookeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization).命 ...
- np.mean()函数
1. 数组的操作: import numpy as np a = np.array([[1, 2], [3, 4]]) print(a) print(type(a)) print(np.mean(a) ...
- go 学习 (四):接口 & 方法
接口声明 // 接口声明 语法:接口是一个 函数签名 的集合,函数签名(函数的声明,不包括实现) type interfaceName interface { method1(param param ...
- LeetCode 841. Keys and Rooms
原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...
- pgloader 学习(四)一些简单操作例子
上边已经说明了pgloader 的基本使用(篇理论),但是对于实际操作偏少,以下是一个简单的操作 不像官方文档那样,我为了方便,直接使用docker-compose 运行,同时这个环境,会在后边大部分 ...
- java web项目改装exe安装版
https://blog.csdn.net/rico_zhou/article/details/79868129java简单程序打包成exe https://blog.csdn.net/rico_zh ...