Skip to content

Commit

Permalink
2019-08-31
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Sep 1, 2019
1 parent 3771c05 commit fe9c322
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
48 changes: 48 additions & 0 deletions 0273.整数转换英文表示/0273-整数转换英文表示.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
def helper(num):
n = int(num)
num = str(n)
if n < 100:
return subhelper(num)
else:
return ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred " + subhelper(num[1:]) if num[1:] != "00" else ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred"

def subhelper(num):
n = int(num)
l1 = ["Zero", "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"]
if n < 10:
return l1[int(num)]
if 10 <= n < 20:
return l2[n - 10]
if 20 <= n < 100:
return l3[int(num[0]) - 2] + " " + l1[int(num[1])] if num[1] != "0" else l3[int(num[0]) - 2]
res = ""
if num >= 1000000000:
res = helper(str(num)[0]) + " Billion"
if str(num)[1:4] != "000":
res += " " + helper(str(num)[1:4]) + " Million"
if str(num)[4:7] != "000":
res += " " + helper(str(num)[4:7]) + " Thousand"
if str(num)[7:] != "000":
res += " " + helper(str(num)[7:])
elif num >= 1000000:
res = helper(str(num)[:-6]) + " Million"
if str(num)[-6:-3] != "000":
res += " " + helper(str(num)[-6:-3]) + " Thousand"
if str(num)[-3:] != "000":
res += " " + helper(str(num)[-3:])
elif num >= 1000:
res = helper(str(num)[:-3]) + " Thousand"
if str(num)[-3:] != "000":
res += " " + helper(str(num)[-3:])
else:
return helper(str(num))

return res
8 changes: 4 additions & 4 deletions 1165.单行键盘/1165-单行键盘.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def calculateTime(self, keyboard, word):
for i, char in enumerate(keyboard):
dic[char] = i

res, last_pos = 0, 0
res, cur_pos = 0, 0
for char in word:
res += abs(dic[char] - last_pos)
last_pos = dic[char]
return res
res += abs(dic[char] - cur_pos)
cur_pos = dic[char]
return res

0 comments on commit fe9c322

Please sign in to comment.