- 博客(97)
- 收藏
- 关注
原创 Leetcode111. 二叉树的最小深度 Minimum Depth of Binary Tree - Python 以迭代法解题
这一步最重要 只需要:若判断左孩子和右孩子同时为空时,则返回当前depth。其它和层序遍历二叉树思路一致。
2022-09-24 22:39:01
204
原创 Leetcode104. 二叉树的最大深度 Maximum Depth of Binary Tree - Python 以迭代/递归法解题
注意全局变量self.res的使用,以及depth+1 因为深度和depth始终差1。同迭代层序遍历二叉树。
2022-09-24 17:48:06
184
原创 Leetcode117. 填充每个节点的下一个右侧节点指针 II Populating Next Right Pointers in Each Node II - Python 以迭代法解题
同Leetcode116。
2022-09-24 17:29:41
142
原创 Leetcode116. 填充每个节点的下一个右侧节点指针 Populating Next Right Pointers in Each Node - Python 以迭代法解题
遍历到每行非最后节点时,将cur.next指向queue[0], 注意这一步不能queue.popleft,否则就把下一层待遍历的孩子节点给弹出了。在遍历到每行最后一个节点时,break;把result和results都去掉;
2022-09-24 16:55:54
134
原创 Leedcode515.在每个树行中找最大值 Find Largest Value in Each Tree Row - Python 以迭代/递归法实现
【代码】Leedcode515.在每个树行中找最大值 Find Largest Value in Each Tree Row - Python 以迭代/递归法实现。
2022-09-24 16:09:35
152
原创 Leetcode429. N叉数的层序遍历 N-ray Tree Level Order Traversal - Python 以迭代法实现
2.将该层各根节点的孩子们,依次入deque,供下一轮while里的for遍历;与二叉树层序遍历不同的是,这里的孩子节点是由Node节点以列表的形式保存的。3.依次遍历在上一层加入到deque的孩子们;
2022-09-24 15:54:44
193
原创 Leetcode637. 二叉树的层平均值 Average of Levels in Binary Tree - Python 以递归/迭代算法实现
很简单,就等于层序遍历二叉树。
2022-09-24 14:45:21
751
原创 Leetcode199. 二叉树的右视图 Binary Tree Right Side View - Python 以递归/迭代算法实现
注意不要看到是右视图,就忽略遍历左孩子if root.left: traversal(root.left, depth + 1)。2.在for循环体里,依次将每个节点的左右孩子(下一层节点)加入到queue,待下一轮while用;当一个根节点没有右孩子时,左孩子也能被看到。如此,无需遍历每层非最右侧的子节点的值,节约了时间复杂度。本题就是考察二叉树的层序遍历而已,没什么难的。迭代法层序遍历二叉树。
2022-09-24 13:12:34
804
原创 Leetcode107. 二叉树的层序遍历 II Binary Tree Level Order Traversal II - Python 以递归/迭代算法实现
只需将resutlts数组颠倒顺序即可。只需将resutlts数组颠倒顺序即可。
2022-09-24 11:22:13
686
原创 Leetcode102. 二叉树的层序遍历 Binary Tree Level Order Traversal - Python 以递归/迭代算法实现
2.函数体具体操作:当遇到新层,则添加[];遇到父节点就将root.val输出;然后分别将左孩子、右孩子依次用遍历父节点一样的方式(递归的方式)遍历孩子节点。3.递归终止条件: #实际上该递归的结束条件为 root.left == None 和 root.right == None。1.调用函数参数:root,depth-当前深度。
2022-09-23 22:00:04
248
原创 Leetcode145. 二叉树的后序遍历 Binary Tree Postorder Traversal - Python 以递归/迭代算法实现
注意每一次递归的逻辑:根据前中后序遍历,调整result.append(root.val)相对其它两步的位置。注意traversal函数的函数参数:root根节点。注意结束递归条件:当root == None。
2022-09-21 23:28:57
254
原创 Leetcode94. 二叉树的中序遍历 Binary Tree Inorder Traversal - Python 以递归/迭代算法实现
注意每一次递归的逻辑:根据前中后序遍历,调整result.append(root.val)相对其它两步的位置。注意traversal函数的函数参数:root根节点。注意结束递归条件:当root == None。
2022-09-21 23:23:21
283
原创 Leetcode144. 二叉树的前序遍历 Binary Tree Preorder Traversal - Python 以递归/迭代算法实现
注意每一次递归的逻辑:根据前中后序遍历,调整result.append(root.val)相对其它两步的位置。注意traversal函数的函数参数:root根节点。注意结束递归条件:当root == None。
2022-09-21 22:49:07
391
原创 Leetcode347. 前 K 个高频元素 Top K Frequent Elements - Python 以小顶堆实现
Leetcode347. 前 K 个高频元素 Top K Frequent Elements - Python 以小顶堆实现
2022-09-21 12:01:56
444
原创 Leetcode239. 滑动窗口最大值 Sliding Window Maximum - Python 以单调队列实现
注意理解 在压入新元素时,如果待压入元素比当前队列中的元素都大,(必须是>,>=不能通过某些示例) 则需要把队列中的元素都踢出去。这样的话才能保证最大的元素始终在单调队列的左出口。注意理解 只有当待踢出元素==单调队列最左边元素时,才真正将单调队列最左边元素踢出。其它情况下,待提出元素在push函数中,已经被踢出去了,不需要再踢一次。注意理解 1.单调队列是从右至左单调递减或递增的,此处是递减。2.单调队列最右边的出口始终是滑动窗口中最大的元素。
2022-09-13 10:47:21
463
原创 Leetcode150. 逆波兰表达式求值 Evaluate Reverse Polish Notation - Python 以栈实现
class Solution: def evalRPN(self, tokens: List[str]) -> int: stack = [] for i in tokens: if i in {"+","-","*","/"}: last = stack.pop() first = stack.pop() .
2022-03-30 11:14:00
1210
原创 Leetcode 459. 重复的子字符串 Repeated Substring Pattern - Python KMP算法求next数组
class Solution: def repeatedSubstringPattern(self, s: str) -> bool: l = len(s) next = self.getNext(s) if next[l-1] != 0 and l % ( l- next[l-1]) == 0: return True #重复某一字段构成的数组的特点可以被if条件检查出来 abcdabcdabcd .
2022-03-29 23:06:02
203
原创 Leetcode 1047.删除字符串中的所有相邻重复项 Remove All Adjacent Duplicates In String - Python 以栈实现
class Solution: def removeDuplicates(self, s: str) -> str: stack = [] for i in s: if not stack or stack[-1] != i: #当 栈为空 或 待入栈元素与栈顶元素不等时 入栈 stack.append(i) else: #.
2022-03-29 23:05:46
614
原创 Leetcode 20.有效的括号 Valid Parentheses - Python
class Solution: def isValid(self, s: str) -> bool: stack = [] for i in s: if i == "(": stack.append(")") elif i == "[": stack.append("]") elif i == "{": .
2022-03-27 21:51:42
932
原创 Leetcode 225. 用队列实现栈 Implement Stack using Queues - Python
from collections import dequeclass MyStack: def __init__(self): self.queue_in = deque() self.queue_out = deque() def push(self, x: int) -> None: self.queue_in.append(x) def pop(self) -> int: if self..
2022-03-26 13:56:56
609
原创 Leetcode 232. 用栈实现队列 Implement Queue using Stacks - Python 用双栈实现队列
class MyQueue: def __init__(self): self.stack_in = [] self.stack_out = [] def push(self, x: int) -> None: self.stack_in.append(x) def pop(self) -> int: if self.empty(): return None .
2022-03-26 12:38:45
1039
原创 Leetcode 459. 重复的子字符串 Repeated Substring Pattern - Python KMP算法求next数组
class Solution: def repeatedSubstringPattern(self, s: str) -> bool: l = len(s) next = self.getNext(s) if next[l-1] != 0 and l % ( l- next[l-1]) == 0: return True #重复某一字段构成的数组的特点可以被if条件检查出来 abcdabcdabcd .
2022-03-26 11:04:08
108
原创 Leetcode 28. 实现 strStr() Implement strStr() - Python - KMP算法
class Solution: def strStr(self, haystack: str, needle: str) -> int: a = len(haystack) b = len(needle) if b == 0: return 0 next = self.getnext(b,needle) j=0 for i in range(a): .
2022-03-25 21:37:32
691
原创 Leetcode 剑指 Offer 58 - II. 左旋转字符串 LCOF - Python
class Solution: def reverseLeftWords(self, s: str, n: int) -> str: m = len(s) temp = list(s) temp.extend(['']*n) front, rear = 0, m for i in range(0,n): temp[rear] = temp[front] rear +.
2022-03-22 20:35:19
359
原创 Leetcode 151.颠倒字符串中的单词 Reverse Words in a String - Python 双指针法
class Solution: def reverseWords(self, s: str) -> str: def trim_space(s:str): res = [] front = 0 rear = len(s)-1 while s[front] == ' ': front += 1 while s[rear] == .
2022-03-22 17:17:09
656
原创 Leetcode 剑指 Offer 05. 替换空格 LCOF - Python
class Solution: def replaceSpace(self, s: str) -> str: counter = s.count(' ') res = list(s) res.extend([' ']*counter*2) front = len(s)-1 rear = len(res)-1 while front >= 0: if res[front.
2022-03-22 15:02:34
561
原创 Leetcode 541.反转字符串II Reverse String II - Python
class Solution: def reverseStr(self, s: str, k: int) -> str: remain = len(s) res = list(s) def swap(front:int,rear:int) -> str: while front < rear: res[front], res[rear] = res[rear], res[fro.
2022-03-21 23:00:05
302
原创 Leetcode 344.反转字符串 Reverse String - Python
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ front = 0 n = len(s) rear = n - 1 for i in range(n): if fr.
2022-03-21 18:13:11
349
原创 Leetcode 18.四数之和 4Sum - Python 双指针法
class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: result = [] nums.sort() length = len(nums) for i in range(length-3): if i == 0 or nums[i] != nums[i-1]: #当nums[i]与nums[.
2022-03-02 12:16:55
406
原创 双指针法为什么不适用于Leetcode 1.Two Sum 两数之和? - Python
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: result = [] nums.sort() for i in range(len(nums)): front = i rear = len(nums)-1 while front < rear: .
2022-02-28 21:49:34
265
原创 Leetcode 15. 三数之和 3Sum - Python 双指针法
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: result = []#将数组从小到大排序,便于双指针法的实施 nums.sort() for i in range(len(nums)): if i == 0 or nums[i] != nums[i-1]: #第一个元素 或 跟前一个i不相同的i才能进入(去重) .
2022-02-28 21:25:49
501
原创 Leetcode 383. 赎金信 Ransom Note - Python 哈希法
class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: hash = [0] *26 for c in magazine: hash[ord(c)- ord('a')] += 1 for c in ransomNote: if hash[ord(c)-ord('a')] == .
2022-02-28 12:09:34
347
原创 Leetcode 454. 四数相加 II 4Sum II - Python 哈希法
class Solution: def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: hash = dict()#统计nums1和nums2所有potential sum 的次数,将其放到哈希表中 for n1 in nums1: for n2 in nums2: .
2022-02-28 11:38:31
318
原创 Leetcode 1.两数之和 Two Sum - Python 哈希法
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: pool = dict() for idx, num in enumerate(nums): if target - num not in pool: pool[num] = idx else: r.
2022-02-28 10:44:56
380
原创 Leetcode 202. 快乐数 Happy Number - Python 哈希法
class Solution: def isHappy(self, n: int) -> bool: sum_pool = set() num = str(n) sum = 0 while sum != 1: sum = 0 for i,c in enumerate(num): sum += pow(int(c),2) if.
2022-02-28 10:21:59
339
原创 Leetcode 349.两个数组的交集 Intersection of Two Arrays - Python 哈希法
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(set(nums2)))当数值较大时,用数组做哈希表是不明智的选择。此时可以选择set集合数据结构了。set的元素是无序的,不可更改的,无索引下标的。详情看Python Set 的W3School具体介绍...
2022-02-27 23:00:02
355
原创 Leetcode 1002. 查找共用字符 Find Common Characters - Python 哈希法
class Solution: def commonChars(self, words: List[str]) -> List[str]: if not words : return [] result = [] hash = [0]*26#记录数组中第一个字符串各字母出现频率 for i, c in enumerate(words[0]): hash[ord(c)-ord('a')] += 1#记录.
2022-02-26 23:20:00
371
原创 Leetcode 9. 回文数 Palindrome Number - Python 双指针法
class Solution: def isPalindrome(self, x: int) -> bool: to_string = str(x) i = 0 q = len(to_string)-1 front = to_string[i] rear = to_string[q] while front == rear: if i==q or abs(i-q)=.
2022-02-26 13:57:47
441
原创 Leetcode 242. 有效的字母异位词 Valid Anagram - Python 哈希法
class Solution: def isAnagram(self, s: str, t: str) -> bool: record = [0] *26 for i in s:# 用哈希法( 哈希函数:ord(i)-ord('a') )将'a'映射为0 'b'映射为1..'z'映射为25 其它字母同理 record[ord(i)-ord('a')] += 1 #此处不用记住ASCII 用ord()计算即可 f.
2022-02-25 21:15:19
301
原创 Leetcode 面试题 02.07. 链表相交 Intersection of Two Linked Lists LCCI - Python 双指针法
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: curA, curB =.
2022-02-24 22:19:19
298
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅