273. Integer to English Words
class Solution:
def numberToWords(self, num: int) -> str:
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):
# print(p,w)
if n<1000**(p+1):
return words(n//(1000**p))+[w]+words(n%(1000**p))
return ' '.join(words(num)) or 'Zero'
模拟题
for p,w in enumerate(('Thousand','Million','Billion'),1):文章来源:https://www.toymoban.com/news/detail-814005.html
p从1开始文章来源地址https://www.toymoban.com/news/detail-814005.html
到了这里,关于273. Integer to English Words的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!