classSolution: deflengthOfLongestSubstring(self, s: str) -> int: # 回溯 + set n = len(s) que = collections.deque() iflen(s) == 0: return0 que.append(s[0]) res = 1 tmp = 1 for i inrange(1, n): if s[i] notin que: tmp += 1 else: while que and que.popleft() != s[i]: tmp -= 1 que.append(s[i]) res = max(res, tmp) return res deflengthOfLongestSubstring(self, s: str) -> int: # 双指针 + 哈希表 dic, res, i = {}, 0, -1 for j inrange(len(s)): if s[j] in dic: i = max(dic[s[j]], i) # 更新左指针 i dic[s[j]] = j # 哈希表记录 res = max(res, j - i) # 更新结果 return res
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defdeleteNode(self, head: ListNode, val: int) -> ListNode: ifnot head: return head cur = dummy = ListNode(0, head) while cur.next: if cur.next.val == val: cur.next = cur.next.next break else: cur = cur.next return dummy.next