Skip to content

Commit fe9c322

Browse files
committedSep 1, 2019
2019-08-31
1 parent 3771c05 commit fe9c322

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed
 
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution(object):
2+
def numberToWords(self, num):
3+
"""
4+
:type num: int
5+
:rtype: str
6+
"""
7+
def helper(num):
8+
n = int(num)
9+
num = str(n)
10+
if n < 100:
11+
return subhelper(num)
12+
else:
13+
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"
14+
15+
def subhelper(num):
16+
n = int(num)
17+
l1 = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
18+
l2 = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
19+
l3 = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
20+
if n < 10:
21+
return l1[int(num)]
22+
if 10 <= n < 20:
23+
return l2[n - 10]
24+
if 20 <= n < 100:
25+
return l3[int(num[0]) - 2] + " " + l1[int(num[1])] if num[1] != "0" else l3[int(num[0]) - 2]
26+
res = ""
27+
if num >= 1000000000:
28+
res = helper(str(num)[0]) + " Billion"
29+
if str(num)[1:4] != "000":
30+
res += " " + helper(str(num)[1:4]) + " Million"
31+
if str(num)[4:7] != "000":
32+
res += " " + helper(str(num)[4:7]) + " Thousand"
33+
if str(num)[7:] != "000":
34+
res += " " + helper(str(num)[7:])
35+
elif num >= 1000000:
36+
res = helper(str(num)[:-6]) + " Million"
37+
if str(num)[-6:-3] != "000":
38+
res += " " + helper(str(num)[-6:-3]) + " Thousand"
39+
if str(num)[-3:] != "000":
40+
res += " " + helper(str(num)[-3:])
41+
elif num >= 1000:
42+
res = helper(str(num)[:-3]) + " Thousand"
43+
if str(num)[-3:] != "000":
44+
res += " " + helper(str(num)[-3:])
45+
else:
46+
return helper(str(num))
47+
48+
return res

‎1165.单行键盘/1165-单行键盘.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def calculateTime(self, keyboard, word):
99
for i, char in enumerate(keyboard):
1010
dic[char] = i
1111

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

0 commit comments

Comments
 (0)