classSolution_1: defpartitionLabels(self, s: str) -> List[int]: str_dict = {} output_lst = [] for i, v inenumerate(s): if v in str_dict.keys(): str_dict[v][1] = i
else: str_dict[v] = [i, i] temp_lst = [] for k, v in str_dict.items(): temp_lst.append(v)
for i inrange(len(temp_lst) - 1): if curr_max < temp_lst[i + 1][0]: output_lst.append(curr_max - curr_low + 1) curr_low = temp_lst[i + 1][0] curr_max = temp_lst[i + 1][1] elif curr_max < temp_lst[i + 1][1]: curr_max = temp_lst[i + 1][1] output_lst.append(curr_max - curr_low + 1) return output_lst classSolution_2: defpartitionLabels(self, s: str) -> List[int]: str_dict = {} output_lst = [] for i, v inenumerate(s): str_dict[v] = i
start = 0 end = str_dict[s[0]] for i inrange(1, len(s)): if i > end: output_lst.append(end - start + 1) start = i end = str_dict[s[i]] if end < str_dict[s[i]]: end = str_dict[s[i]]
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: defdetectCycle(self, head: ListNode) -> ListNode: fast, slow, very_slow = head, head, head while head and fast and fast.next: fast = fast.next.next slow = slow.next
if fast == slow: whileTrue: if slow == very_slow: return slow very_slow = very_slow.next slow = slow.next
classSolution: defjudgeSquareSum(self, c: int) -> bool: u = int(math.sqrt(c)) i = 0 while i <= u: if (i**2 + u**2) > c: u -= 1 elif (i**2 + u**2) < c: i += 1 else: returnTrue returnFalse
# if no import classSolution_2: defjudgeSquareSum(self, c: int) -> bool: i = 0 u = 0 while u ** 2 < c: u += 1 while i <= u: if (i**2 + u**2) > c: u -= 1 elif (i**2 + u**2) < c: i += 1 else: returnTrue returnFalse
680 Easy
双指针,判断s[i] != s[j], 这时我们有2种选择,删掉i,或者删掉j,假如说删掉i,i+1 不等于j则说明不能删i同样apply to j。接着我们判断删掉之后的东西是不是为一个回文字符串就可以了。 当出现i和j都可以被删除的情况只要判断删掉2个中又一个为回文字符串就行。
deffind_in_s(word): i = 0 j = 0 while i < len(s): if word[j] == s[i]: j += 1 if j >= len(word): returnTrue
i += 1
returnFalse
dictionary.sort(key=lambda x: (len(x), x)) # for word in dictionary: # word_in_dict = find_in_s(word) # if (len(word) > len(curr_word)) and word_in_dict: # curr_word = word # elif (len(word) == len(curr_word)) and (word < curr_word) and word_in_dict: # curr_word = word
for word in dictionary: if (len(word) > len(curr_word)) and find_in_s(word): curr_word = word
return curr_word
287 Medium
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: deffindDuplicate(self, nums: List[int]) -> int: s, f = nums[0], nums[nums[0]] while f < len(nums): if s == f: f = 0 while f != s: s = nums[s] f = nums[f]
return f
s = nums[s] f = nums[nums[f]]
return nums[0]
Binary Search
69 Easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution: defmySqrt(self, x: int) -> int: left, right = 0, x
if x == 1: left = 1 while (right - left) > 1: mid_point = int((right + left) / 2) if x / mid_point == mid_point: return mid_point elif x / mid_point < mid_point: right = mid_point else: left = mid_point
# The isBadVersion API is already defined for you. # @param version, an integer # @return an integer # def isBadVersion(version):
classSolution: deffirstBadVersion(self, n): """ :type n: int :rtype: int """ l, r = 1, n while l <= r: mid = l + (r - l) // 2 if isBadVersion(mid): r = mid - 1 else: l = mid + 1
iflen(num1_dict) > len(num2_dict): for k, v in num2_dict.items(): if k in num1_dict.keys(): if v > num1_dict[k]: result_lst = result_lst + [k] * num1_dict[k] else: result_lst = result_lst + [k] * v else: for k, v in num1_dict.items(): if k in num2_dict.keys(): if v > num2_dict[k]: result_lst = result_lst + [k] * num2_dict[k] else: result_lst = result_lst + [k] * v return result_lst
154 Easy
class Solution: def minArray(self, numbers: List[int]) -> int: for i in range(len(numbers) - 1): if numbers[i] > numbers[i + 1]: return numbers[i + 1]
return numbers[0]
35 Easy
不需要最后的l边界判断
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defsearchInsert(self, nums: List[int], target: int) -> int: l, r = 0, len(nums) - 1 while l <= r: mid = l + (r - l) // 2# prevent overflow if nums[mid] == target: return mid elif nums[mid] < target: l = mid + 1 elif nums[mid] > target: r = mid - 1 return l
374 Easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# The guess API is already defined for you. # @param num, your guess # @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 # def guess(num: int) -> int:
classSolution: defguessNumber(self, n: int) -> int: l, r = 1, n while l <= r: mid = l + (r - l) // 2 if guess(mid) == 0: return mid elif guess(mid) < 0: r = mid - 1 elif guess(mid) > 0: l = mid + 1
744 Easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defnextGreatestLetter(self, letters: List[str], target: str) -> str: l, r = 0, len(letters) - 1 while l <= r: mid = l + (r - l) // 2 if letters[mid] > target: r = mid - 1 else: l = mid + 1 if (r < 0) or (l > len(letters) - 1): return letters[0] else: return letters[l]