摘要: 层序遍历 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 忆象峰飞 阅读(4) 评论(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 忆象峰飞 阅读(8) 评论(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 忆象峰飞 阅读(7) 评论(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 忆象峰飞 阅读(11) 评论(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 忆象峰飞 阅读(12) 评论(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 忆象峰飞 阅读(26) 评论(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 忆象峰飞 阅读(9) 评论(0) 推荐(0) 编辑
摘要: 两两交换链表中的节点 关键点:涉及到头节点变动的都使用虚拟节点。画图找出交换节点指向的顺序和退出循环的条件。 1、迭代法 class Solution: def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: dumm 阅读全文
posted @ 2023-10-17 20:47 忆象峰飞 阅读(9) 评论(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 忆象峰飞 阅读(10) 评论(0) 推荐(0) 编辑
摘要: 一、203.移除链表元素 关键点:如何删除节点,需要知道删除节点前的节点。 1、无虚拟头节点的方法 class Solution: def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: 阅读全文
posted @ 2023-10-16 21:57 忆象峰飞 阅读(11) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示