Skip to content

Commit 714950f

Browse files
committed
2020-02-18
1 parent faa4775 commit 714950f

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

0760.找出变位映射/0760-找出变位映射.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ def anagramMappings(self, A, B):
55
:type B: List[int]
66
:rtype: List[int]
77
"""
8-
record = dict()
9-
for i, b in enumerate(B):
10-
record[b] = i
11-
12-
res = [-1 for _ in range(len(A))]
13-
for i, a in enumerate(A):
14-
res[i] = record[a]
15-
16-
return res
8+
9+
dic = dict()
10+
for i, x in enumerate(B):
11+
dic[x] = i
12+
13+
return [dic[x] for x in A]

0763.划分字母区间/0763-划分字母区间.py

+31-18
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,36 @@ def partitionLabels(self, S):
44
:type S: str
55
:rtype: List[int]
66
"""
7-
dic = {}
7+
from collections import defaultdict
8+
dic = defaultdict(list)
9+
10+
for ch in "abcdefghijklmnopqrstuvwxyz":
11+
for i, char in enumerate(S):
12+
if char == ch:
13+
dic[ch].append(i)
14+
break
15+
16+
for i in range(len(S) - 1, -1, -1):
17+
if S[i] == ch:
18+
dic[ch].append(i)
19+
break
20+
21+
22+
intervals = []
23+
for val in dic.values():
24+
intervals.append(val)
825

9-
for index, char in enumerate(S):
10-
dic[char] = index
11-
12-
right = dic[S[0]]
13-
left = 0
26+
intervals.sort()
27+
#print intervals
28+
1429
res = []
15-
for index, char in enumerate(S):
16-
right = max(right, dic[char])
17-
if index >= right:
18-
res.append(right - left + 1)
19-
left = right + 1
20-
21-
return res
22-
23-
24-
25-
26-
30+
start, end = 0, 0
31+
for s, e in intervals:
32+
if s > end:
33+
res.append(end - start + 1)
34+
start, end = s, e
35+
else:
36+
end = max(e, end)
37+
res.append(end - start + 1)
38+
39+
return res
+11-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class Solution(object):
22
def numJewelsInStones(self, J, S):
3-
if len(J) == 0 or len(S) == 0:
4-
return 0
5-
count = 0
6-
for itemins in S:
7-
if itemins in J:
8-
count += 1
9-
10-
return count
11-
3+
"""
4+
:type J: str
5+
:type S: str
6+
:rtype: int
7+
"""
8+
J = set(J)
9+
res = 0
10+
for s in S:
11+
if s in J:
12+
res += 1
13+
return res

0 commit comments

Comments
 (0)