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 One"
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
if num==0:
return 'Zero'
l1 = ['','One ','Two ','Three ','Four ','Five ','Six ','Seven ','Eight ','Nine ']
l2 = ['Ten ','Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ','Sixteen ','Seventeen ','Eighteen ','Nineteen ']
l3 = ['','','Twenty ','Thirty ','Forty ','Fifty ','Sixty ','Seventy ','Eighty ','Ninety ']
l4 = ['','Hundred ','Thousand ','Million ','Billion ']
s = str(num)
def solve(n,i):
res = ''
if n=='000':
return res
if n[0] != '0':
res += l1[int(n[0])] + l4[1]
if n[1] == '1':
res += l2[int(n[2])%10]
else:
res += l3[int(n[1])] + l1[int(n[2])]
return res + l4[i]
s = '0' * (12-len(s)) + s
output = solve(s[0:3],4) + solve(s[3:6],3) + solve(s[6:9],2) + solve(s[9:12],0)
return output.rstrip()
tip:补到同样的12位方便处理