10 2023 档案

摘要:二叉搜素树,利用中序遍历的升序结果 530.二叉搜索树的最小绝对差 1、递归中序遍历 class Solution: def __init__(self): self.pre = None self.res = float('inf') def getMinimumDifference(self, 阅读全文
posted @ 2023-10-31 16:55 忆象峰飞 阅读(4) 评论(0) 推荐(0) 编辑
摘要:654.最大二叉树 1、使用切片 class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: if len(nums) == 0: return None max_val = 阅读全文
posted @ 2023-10-30 22:43 忆象峰飞 阅读(6) 评论(0) 推荐(0) 编辑
摘要:513.找树左下角的值 1、层序遍历迭代法 def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: queue = [root] res = float('-inf') while queue: n = len(queue) f 阅读全文
posted @ 2023-10-30 21:00 忆象峰飞 阅读(4) 评论(0) 推荐(0) 编辑
摘要:110.平衡二叉树 1、递归法 class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: if self.get_height(root) != -1: # -1 代表高度差大于 1 return True els 阅读全文
posted @ 2023-10-30 16:35 忆象峰飞 阅读(5) 评论(0) 推荐(0) 编辑
摘要:104.二叉树的最大深度 1、后续遍历递归法 class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if root is None: return 0 left_depth = self.maxDepth(root. 阅读全文
posted @ 2023-10-28 11:02 忆象峰飞 阅读(4) 评论(0) 推荐(0) 编辑
摘要:前序遍历 统一写法用None 来区分遍历查找的节点和处理节点 1、递归法 class Solution: def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: res = [] self.preorder(root, 阅读全文
posted @ 2023-10-25 20:34 忆象峰飞 阅读(5) 评论(0) 推荐(0) 编辑
摘要:层序遍历 1、迭代法,使用队列 class Solution: def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: res = [] if root is None: return res queue = [root] 阅读全文
posted @ 2023-10-24 21:37 忆象峰飞 阅读(2) 评论(0) 推荐(0) 编辑
摘要:239. 滑动窗口最大值 class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res = [] tmp = MyQueue() for i in range(k): tmp.push(nu 阅读全文
posted @ 2023-10-23 20:31 忆象峰飞 阅读(6) 评论(0) 推荐(0) 编辑
摘要:20. 有效的括号 思路:分析出三种情况,画图模拟。写代码容易写错。 class Solution: def isValid(self, s: str) -> bool: a_stack = list() for i in s: if i == '(': a_stack.append(')') el 阅读全文
posted @ 2023-10-22 21:12 忆象峰飞 阅读(4) 评论(0) 推荐(0) 编辑
摘要:实现 strStr() kmp 算法 一、暴力解法 class Solution: def strStr(self, haystack: str, needle: str) -> int: m, n = len(haystack), len(needle) for i in range(m): if 阅读全文
posted @ 2023-10-21 15:41 忆象峰飞 阅读(9) 评论(0) 推荐(0) 编辑
摘要:232.用栈实现队列 class MyQueue: def __init__(self): self.stack_in = list() self.stack_out = list() def push(self, x: int) -> None: self.stack_in.append(x) d 阅读全文
posted @ 2023-10-21 15:38 忆象峰飞 阅读(10) 评论(0) 推荐(0) 编辑
摘要:344.反转字符串 双指针法 时间复杂度为: O(n), 空间复杂度为: O(1) class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place 阅读全文
posted @ 2023-10-18 21:19 忆象峰飞 阅读(25) 评论(0) 推荐(0) 编辑
摘要:454.四数相加II 关键点:减少复杂度判断 a+b 是否 等于 -(d+e).求和类的题目,利用好相反数。 class Solution: def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nu 阅读全文
posted @ 2023-10-17 23:01 忆象峰飞 阅读(8) 评论(0) 推荐(0) 编辑
摘要:两两交换链表中的节点 关键点:涉及到头节点变动的都使用虚拟节点。画图找出交换节点指向的顺序和退出循环的条件。 1、迭代法 class Solution: def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: dumm 阅读全文
posted @ 2023-10-17 20:47 忆象峰飞 阅读(7) 评论(0) 推荐(0) 编辑
摘要:242.有效的字母异位词 1、数组法 这个思路贼 6 ,在这个题的效率也高 class Solution: def isAnagram(self, s: str, t: str) -> bool: # 全部转为 asii 码 如果是互为异为词,则最后的 -+ 后的结果为 0 record = [0] 阅读全文
posted @ 2023-10-16 23:45 忆象峰飞 阅读(8) 评论(0) 推荐(0) 编辑
摘要:一、203.移除链表元素 关键点:如何删除节点,需要知道删除节点前的节点。 1、无虚拟头节点的方法 class Solution: def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: 阅读全文
posted @ 2023-10-16 21:57 忆象峰飞 阅读(10) 评论(0) 推荐(0) 编辑
摘要:977.有序数组的平方 暴力求解(O(n+logn)) class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: return sorted(i**2 for i in nums) 双指针(O(n)) 由于列表是单调 阅读全文
posted @ 2023-10-12 21:10 忆象峰飞 阅读(10) 评论(0) 推荐(0) 编辑
摘要:题目链接:704 二分查找 关键点思路: 1、是否要进入到 while 部分的代码是 left <= right 还是 left < right, 看 [left, right] 是否是合法区间. 例如 [1, 1] 是合法区间,取<=; [1, 1) 非合法区间,取 < 。 2、缩小区间时,考虑边 阅读全文
posted @ 2023-10-11 22:28 忆象峰飞 阅读(18) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示