273. Integer to English Words
来源:互联网 发布:linux设备驱动功能 编辑:程序博客网 时间:2024/05/21 10:12
题意: 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)
思路:这题如果没给hint的话,还是要花挺多时间去把细节考虑清楚的,这题我的解法太麻烦了,就当记录一下吧:
def numberToWords(self, num): """ :type num: int :rtype: str """ if num == 0: return 'Zero' single_unit = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve',"Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"] tens_digit = ["Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety",'Hundred'] base_digit = ['','Thousand','Million','Billion'] s = (10-len(str(num)))*'0'+str(num) s = map(int,s) ans = single_unit[s[0]]+' '+base_digit[-1]+' ' if s[0]>0 else '' for i in xrange(3): if s[3*i+1]+s[3*i+2]+s[3*i+3]: ans += single_unit[s[3*i+1]]+' '+tens_digit[-1]+' ' if single_unit[s[3*i+1]] else '' if s[3*i+2]>1: ans += tens_digit[s[3*i+2]-2] + ' ' ans += single_unit[s[3*i+3]]+' ' if single_unit[s[3*i+3]] else '' else: ans += single_unit[10*s[3*i+2]+s[3*i+3]]+' ' if 10*s[3*i+2]+s[3*i+3] else '' ans += base_digit[-(i+2)]+' ' return ans.strip()
而且逻辑性挺差的,这题就应该把英文构成一个列表,最后join一下,逻辑会清晰很多,这里贴一个清晰的参考代码:
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]
特别是结尾的lookup[(num / 10) * 10] + ” ” + lookup[num % 10],直接把非字典中的十位和个位分开,比如83,直接lookup(80)+’ ‘+lookup(3),写法很棒。最后再放一个递归的写法,也挺有意思的:
def numberToWords(self, num): to19 = 'One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve ' \ 'Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen'.split() tens = 'Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety'.split() def words(n): if n < 20: return to19[n-1:n] if n < 100: return [tens[n/10-2]] + words(n%10) if n < 1000: return [to19[n/100-1]] + ['Hundred'] + words(n%100) for p, w in enumerate(('Thousand', 'Million', 'Billion'), 1): if n < 1000**(p+1): return words(n/1000**p) + [w] + words(n%1000**p) return ' '.join(words(num)) or 'Zero'这里enumerate(*,1)中的1是指w从1开始增加。
0 0
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- 273. Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- Integer to English Words
- java DES 加密解密算法及报错 Given final block not properly padded
- UVa 12657:Boxes in a Line
- shell 中的sleep命令
- Unity Mesh(二) Mesh画立方体Cube和八面体Octahedron
- 一文读懂机器学习,大数据/自然语言处理/算法全有了……
- 273. Integer to English Words
- 11111
- 程序员技术练级攻略
- 移动端字体设置最佳实践
- T-SQL - left join, right join, inner join, outer join的区别
- redis命令(hash)
- 传输层之UDP协议
- 深度神经网络中的训练难点(vanishing gradient problem)
- jQuery的全局替换字符串操作