Skip to content

Commit

Permalink
2020-02-18
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Feb 18, 2020
1 parent faa4775 commit 714950f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
15 changes: 6 additions & 9 deletions 0760.找出变位映射/0760-找出变位映射.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ def anagramMappings(self, A, B):
:type B: List[int]
:rtype: List[int]
"""
record = dict()
for i, b in enumerate(B):
record[b] = i

res = [-1 for _ in range(len(A))]
for i, a in enumerate(A):
res[i] = record[a]

return res

dic = dict()
for i, x in enumerate(B):
dic[x] = i

return [dic[x] for x in A]
49 changes: 31 additions & 18 deletions 0763.划分字母区间/0763-划分字母区间.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@ def partitionLabels(self, S):
:type S: str
:rtype: List[int]
"""
dic = {}
from collections import defaultdict
dic = defaultdict(list)

for ch in "abcdefghijklmnopqrstuvwxyz":
for i, char in enumerate(S):
if char == ch:
dic[ch].append(i)
break

for i in range(len(S) - 1, -1, -1):
if S[i] == ch:
dic[ch].append(i)
break


intervals = []
for val in dic.values():
intervals.append(val)

for index, char in enumerate(S):
dic[char] = index

right = dic[S[0]]
left = 0
intervals.sort()
#print intervals

res = []
for index, char in enumerate(S):
right = max(right, dic[char])
if index >= right:
res.append(right - left + 1)
left = right + 1

return res





start, end = 0, 0
for s, e in intervals:
if s > end:
res.append(end - start + 1)
start, end = s, e
else:
end = max(e, end)
res.append(end - start + 1)

return res
20 changes: 11 additions & 9 deletions 0771.宝石与石头/0771-宝石与石头.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
class Solution(object):
def numJewelsInStones(self, J, S):
if len(J) == 0 or len(S) == 0:
return 0
count = 0
for itemins in S:
if itemins in J:
count += 1

return count

"""
:type J: str
:type S: str
:rtype: int
"""
J = set(J)
res = 0
for s in S:
if s in J:
res += 1
return res

0 comments on commit 714950f

Please sign in to comment.