1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| class Solution(object):
def change3(self, num, num_str): if num in num_str: return num_str[num] result = [] if num > 99: result.append(num_str[num/100]) result.append('Hundred') num %= 100 if num > 9: if num in num_str: result.append(num_str[num]) num = 0 else: result.append(num_str[num-num%10]) num %= 10 if num !=0: result.append(num_str[num]) return ' '.join(result)
def numberToWords(self, num): """ :type num: int :rtype: str """ num_str = {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', 1000:'Thousand', 1000000:'Million', 1000000000:'Billion'} if num == 0: return 'Zero' result = [] t = 1000000000 while t!=1 and num!=0: if num / t != 0: result.append(self.change3(num/t, num_str)) result.append(num_str[t]) num %= t t /= 1000 #print num,t if num != 0: result.append(self.change3(num, num_str)) return ' '.join(result)
|
评论系统未开启,无法评论!