diff --git "a/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" "b/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" index ea4504e..58c9a94 100644 --- "a/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" +++ "b/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" @@ -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 \ No newline at end of file + + dic = dict() + for i, x in enumerate(B): + dic[x] = i + + return [dic[x] for x in A] \ No newline at end of file diff --git "a/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" "b/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" index 8b1001d..8216656 100644 --- "a/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" +++ "b/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" @@ -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 - - - - - \ No newline at end of file + 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 \ No newline at end of file diff --git "a/0771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/0771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" "b/0771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/0771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" index 0060855..4b0e5c9 100644 --- "a/0771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/0771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" +++ "b/0771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/0771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" @@ -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 - \ No newline at end of file + """ + :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 \ No newline at end of file