Skip to content

Commit 7afae5c

Browse files
committed
2019-08-17
1 parent a46c161 commit 7afae5c

File tree

7 files changed

+189
-59
lines changed

7 files changed

+189
-59
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
def diffWaysToCompute(self, input):
3+
"""
4+
:type input: str
5+
:rtype: List[int]
6+
"""
7+
self.record = collections.defaultdict(list)
8+
self.work(input)
9+
return self.record[input]
10+
11+
12+
13+
def work(self, input):
14+
if not input:
15+
return []
16+
if input in self.record:
17+
return self.record[input]
18+
19+
if input.isdigit():
20+
self.record[input] = [int(input)]
21+
return self.record[input]
22+
res = []
23+
24+
for i in range(len(input)):
25+
if input[i] in "+-*":
26+
left = self.diffWaysToCompute(input[:i])
27+
right = self.diffWaysToCompute(input[i + 1:])
28+
29+
for l in left:
30+
for r in right:
31+
if input[i] == "+":
32+
res.append(l + r)
33+
elif input[i] == "-":
34+
res.append(l - r)
35+
else:
36+
res.append(l * r)
37+
38+
self.record[input] = res
39+
return self.record[input]

0243.最短单词距离/0243-最短单词距离.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ def shortestDistance(self, words, word1, word2):
66
:type word2: str
77
:rtype: int
88
"""
9-
# pos1, pos2 = -1,
10-
res = len(words)
11-
pos1, pos2 = -res, -res
12-
for idx, word in enumerate(words):
9+
res = len(words) - 1
10+
pos1, pos2 = -1, -1
11+
for i, word in enumerate(words):
1312
if word == word1:
14-
pos1 = idx
13+
pos1 = i
1514
elif word == word2:
16-
pos2 = idx
17-
else:
18-
continue
19-
# print pos1, pos2, res, abs(pos1 - pos2)
20-
res = min(res, abs(pos1 - pos2))
15+
pos2 = i
16+
if pos1 != -1 and pos2 != -1:
17+
res = min(res, abs(pos1 - pos2))
2118
return res

0244.最短单词距离II/0244-最短单词距离II.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ def __init__(self, words):
44
"""
55
:type words: List[str]
66
"""
7-
from collections import defaultdict
8-
self.words = words
9-
self.record = defaultdict(list)
7+
self.record = collections.defaultdict(list)
108
for i, word in enumerate(words):
119
self.record[word].append(i)
12-
# print self.record
10+
1311
def shortest(self, word1, word2):
1412
"""
1513
:type word1: str
1614
:type word2: str
1715
:rtype: int
1816
"""
19-
res = len(self.words)
20-
for pos1 in self.record[word1]:
21-
for pos2 in self.record[word2]:
22-
res = min(res, abs(pos1 - pos2))
17+
res = float("inf")
18+
p1, p2 = 0, 0
19+
while p1 < len(self.record[word1]) and p2 < len(self.record[word2]):
20+
res = min(res, abs(self.record[word1][p1] - self.record[word2][p2]))
21+
if self.record[word1][p1] < self.record[word2][p2]:
22+
p1 += 1
23+
else:
24+
p2 += 1
2325
return res
24-
2526

2627

2728
# Your WordDistance object will be instantiated and called as such:

0245.最短单词距离III/0245-最短单词距离III.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,31 @@ def shortestWordDistance(self, words, word1, word2):
66
:type word2: str
77
:rtype: int
88
"""
9-
res = len(words)
10-
11-
if word1 != word2:
12-
pos1, pos2 = -1, -1
9+
if word1 == word2:
10+
pre = -1
11+
res = float("inf")
1312
for i, word in enumerate(words):
1413
if word == word1:
15-
pos1 = i
16-
elif word == word2:
17-
pos2 = i
18-
if pos2 != -1 and pos1 != -1:
19-
res = min(res, abs(pos1 - pos2))
14+
if pre != -1:
15+
res = min(res, i - pre)
16+
pre = i
17+
return res
2018
else:
21-
pos = -1
22-
for i, word in enumerate(words):
23-
if word == word1:
24-
if pos != -1:
25-
# print i, pos
26-
res = min(res, i - pos)
27-
pos = i
28-
19+
return self.previousSolution(words, word1, word2)
20+
21+
22+
23+
def previousSolution(self, words, word1, word2):
24+
self.record = collections.defaultdict(list)
25+
for i, word in enumerate(words):
26+
self.record[word].append(i)
27+
res = float("inf")
28+
p1, p2 = 0, 0
29+
list1, list2 = self.record[word1], self.record[word2]
30+
while p1 < len(list1) and p2 < len(list2):
31+
res = min(res, abs(list1[p1]- list2[p2]))
32+
if list1[p1] < list2[p2]:
33+
p1 += 1
34+
else:
35+
p2 += 1
2936
return res

0246.中心对称数/0246-中心对称数.py

+7-23
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,11 @@ def isStrobogrammatic(self, num):
44
:type num: str
55
:rtype: bool
66
"""
7-
8-
#如 0, 1, 6, 8, 9 旋转 180° 以后,得到了新的数字 0, 1, 9, 8, 6 。
9-
#2, 3, 4, 5, 7 旋转 180° 后,得到的不是数字。
10-
if not num:
11-
return True
12-
mapping = {0:0, 1:1, 6:9, 8:8, 9:6}
13-
invalid = [2,3,4,5,7]
14-
15-
N = int(num)
16-
n = N
17-
tmp = 0
18-
res = list()
19-
while(n):
20-
n, tmp = divmod(n, 10)
21-
if tmp in invalid:
7+
mapping = {"0":"0", "1":"1", "6":"9","9":"6", "8":"8"}
8+
newnum = ""
9+
for i, char in enumerate(num):
10+
if char not in mapping:
2211
return False
23-
res.append(mapping[tmp])
24-
25-
res = res[::-1]
26-
r = 0
27-
for i, x in enumerate(res):
28-
r += 10 ** i * x
29-
30-
return r == N
12+
newnum += mapping[char]
13+
14+
return newnum[::-1] == num
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def findStrobogrammatic(self, n):
3+
"""
4+
:type n: int
5+
:rtype: List[str]
6+
"""
7+
record = dict()
8+
record[1] = ["0", "1", "8"]
9+
record[2] = ["11", "69", "88", "96"]
10+
pair = ["00", "11", "88", "69", "96"]
11+
if n <= 2:
12+
return record[n]
13+
cnt = 3
14+
while cnt <= n:
15+
tmp = []
16+
if (cnt - 1) % 2 == 0: #如果前一个是偶数长度,那么直接在中间加长度为1的就可以
17+
for item in record[cnt - 1]:
18+
for num in record[1]:
19+
tmp.append(item[:len(item)// 2] + num + item[len(item) // 2:])
20+
else:
21+
for item in record[cnt - 2]:
22+
for num in pair:
23+
tmp.append(item[:len(item)// 2] + num + item[len(item) // 2:])
24+
record[cnt] = tmp
25+
cnt += 1
26+
return record[n]
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Solution(object):
2+
def strobogrammaticInRange(self, low, high):
3+
"""
4+
:type low: str
5+
:type high: str
6+
:rtype: int
7+
"""
8+
if int(low) > int(high):
9+
return 0
10+
self.findStrobogrammatic(len(high))
11+
#在low里找
12+
low_rec = self.record[len(low)]
13+
#找第一个 >= low的数的下标
14+
low_cnt = 0
15+
for i, num in enumerate(low_rec):
16+
if int(num) >= int(low):
17+
low_cnt = len(low_rec) - i
18+
break
19+
20+
high_rec = self.record[len(high)]
21+
high_cnt = len(high_rec)
22+
for i, num in enumerate(high_rec):
23+
if int(num) > int(high):
24+
high_cnt = i
25+
break
26+
27+
if len(low) + 1 == len(high):
28+
return low_cnt + high_cnt
29+
elif len(low) == len(high):
30+
return low_cnt + high_cnt - len(high_rec)
31+
else:
32+
tmp = 0
33+
for l in range(len(low) + 1, len(high)):
34+
# print l, self.record
35+
tmp += len(self.record[l])
36+
return tmp + low_cnt + high_cnt
37+
#找第一个 > high的数的下标
38+
# left, right = 0, len(low_rec) - 1
39+
# while left < right:
40+
# mid = (left + right) // 2
41+
# if low_rec[mid] == low:
42+
# low_cnt = len(low_rec) - mid
43+
# elif low_rec[mid] > low:
44+
# right = mid - 1
45+
# elif low_rec[mid] < low:
46+
# left = mid + 1
47+
48+
49+
50+
51+
def findStrobogrammatic(self, n):
52+
"""
53+
:type n: int
54+
:rtype: List[str]
55+
"""
56+
self.record = dict()
57+
self.record[0] = ["0"]
58+
self.record[1] = ["0", "1", "8"]
59+
self.record[2] = ["11", "69", "88", "96"]
60+
pair = ["00", "11", "88", "69", "96"]
61+
if n <= 2:
62+
return self.record[n]
63+
cnt = 3
64+
while cnt <= n:
65+
tmp = []
66+
if (cnt - 1) % 2 == 0: #如果前一个是偶数长度,那么直接在中间加长度为1的就可以
67+
for item in self.record[cnt - 1]:
68+
for num in self.record[1]:
69+
tmp.append(item[:len(item)// 2] + num + item[len(item) // 2:])
70+
else: #如果前一个是奇数长度,那么就在中间加长度为2的就可以 ,注意要额外加“00”
71+
for item in self.record[cnt - 2]:
72+
for num in pair:
73+
tmp.append(item[:len(item)// 2] + num + item[len(item) // 2:])
74+
self.record[cnt] = sorted(tmp, key = lambda x: int(x))
75+
cnt += 1

0 commit comments

Comments
 (0)