摘要:链表问题:双指针、迭代法、递归法(天然可用)、虚拟头节点 双链表 既可以向前查询也可以向后查询。 循环链表,顾名思义,就是链表首尾相连。循环链表可以用来解决约瑟夫环问题。 234. 回文链表 1 # Definition for singly-linked list. 2 # class ListN
阅读全文
摘要:35. 搜索插入位置 1 class Solution: 2 def searchInsert(self, nums: List[int], target: int) -> int: 3 left, right = 0, len(nums)-1 4 5 while left <= right: #左
阅读全文
摘要:217. 存在重复元素 1 class Solution: 2 def containsDuplicate(self, nums: List[int]) -> bool: 3 #方法1:set去重,直接比较去重之后数组长度 4 if len(set(nums)) != len(nums): 5 re
阅读全文
摘要:54. 螺旋矩阵 1 class Solution: 2 def spiralOrder(self, matrix: List[List[int]]) -> List[int]: 3 m, n = len(matrix), len(matrix[0]) 4 res = [] #存放遍历后的结果 5
阅读全文
摘要:20. 有效的括号 1 class Solution: 2 #遍历完字符串后,栈是空的,说明全部匹配了 3 def isValid(self, s: str) -> bool: 4 stack = [] 5 #剪枝 6 if len(s) % 2 != 0: 7 return False 8 9 f
阅读全文
摘要:933. 最近的请求次数 1 class RecentCounter: 2 3 def __init__(self): 4 self.q = collections.deque() 5 6 def ping(self, t: int) -> int: 7 self.q.append(t) 8 9 w
阅读全文
摘要:203. 移除链表元素 也可以用栈解决:(程序员小熊) 1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, val=0, next=None): 4 # self.val = val 5
阅读全文
摘要:485. 最大连续 1 的个数 1 class Solution: 2 def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 3 maxCount = count = 0 4 5 for i, num in enumerate(nums)
阅读全文