2025年11月24日

摘要: 链接:121. 买卖股票的最佳时机 - 力扣(LeetCode) 维护到这天为止之前股票的最低价数组 1 class Solution(object): 2 def maxProfit(self, prices): 3 """ 4 :type prices: List[int] 5 :rtype: 阅读全文
posted @ 2025-11-24 22:36 Annetree 阅读(4) 评论(0) 推荐(0)
 
摘要: 链接:46. 全排列 - 力扣(LeetCode) 抄的官方题解 res.append(nums[:]) 这样是可以的,但是res.append(nums)这样不行。 原因是如果使用赋值操作将nums赋值给另一个变量,那么修改新变量的值会影响原始列表nums的值。例如,如果执行新列表变量new_nu 阅读全文
posted @ 2025-11-24 22:18 Annetree 阅读(5) 评论(0) 推荐(0)

2025年11月19日

摘要: 链接:35. 搜索插入位置 - 力扣(LeetCode) 二分+各种边缘条件的判断 1 class Solution(object): 2 def searchInsert(self, nums, target): 3 """ 4 :type nums: List[int] 5 :type targ 阅读全文
posted @ 2025-11-19 22:15 Annetree 阅读(5) 评论(0) 推荐(0)
 
摘要: 链接:200. 岛屿数量 - 力扣(LeetCode) 用深搜把每一个碰到的1都变成0,这样就处理完了一个发现的岛屿 1 class Solution(object): 2 def numIslands(self, grid): 3 """ 4 :type grid: List[List[str]] 阅读全文
posted @ 2025-11-19 15:00 Annetree 阅读(5) 评论(0) 推荐(0)

2025年11月17日

摘要: 链接:169. 多数元素 - 力扣(LeetCode) 找众数,如果排序的话肯定会覆盖大于一半的数,所以2/n的位置肯定是众数 1 class Solution(object): 2 def majorityElement(self, nums): 3 """ 4 :type nums: List[ 阅读全文
posted @ 2025-11-17 15:15 Annetree 阅读(4) 评论(0) 推荐(0)
 
摘要: 链接:226. 翻转二叉树 - 力扣(LeetCode) 递归 1 # Definition for a binary tree node. 2 # class TreeNode(object): 3 # def __init__(self, val=0, left=None, right=None 阅读全文
posted @ 2025-11-17 15:07 Annetree 阅读(4) 评论(0) 推荐(0)
 
摘要: 链接:104. 二叉树的最大深度 - 力扣(LeetCode) 深搜dfs 注意这里递归前要加self. 1 # Definition for a binary tree node. 2 # class TreeNode(object): 3 # def __init__(self, val=0, 阅读全文
posted @ 2025-11-17 14:17 Annetree 阅读(6) 评论(0) 推荐(0)
 
摘要: 链接:94. 二叉树的中序遍历 - 力扣(LeetCode) 递归 1 # Definition for a binary tree node. 2 # class TreeNode(object): 3 # def __init__(self, val=0, left=None, right=No 阅读全文
posted @ 2025-11-17 13:58 Annetree 阅读(3) 评论(0) 推荐(0)
 
摘要: 链接:739. 每日温度 - 力扣(LeetCode) 维护一个单调递减的栈,直到下一个数比栈尾大,就一直弹出,并记录两个下标差值。其他没有记录的默认为0 这个方法可以用于在 O(n) 的时间复杂度内求出数组中各个元素右侧第一个更大或更小的元素及其下标,然后一并得到其他信息。 1 class Sol 阅读全文
posted @ 2025-11-17 13:36 Annetree 阅读(4) 评论(0) 推荐(0)

2025年11月14日

摘要: 链接:155. 最小栈 - 力扣(LeetCode) 在类里面维护一个最小栈,用来记录栈顶到栈底的最小值 1 class MinStack(object): 2 3 def __init__(self): 4 self.stack = [] 5 self.min_stack = [] 6 7 def 阅读全文
posted @ 2025-11-14 22:46 Annetree 阅读(3) 评论(0) 推荐(0)
 
摘要: 链接:20. 有效的括号 - 力扣(LeetCode) python中list就可以直接当栈用 1 class Solution(object): 2 def isValid(self, s): 3 """ 4 :type s: str 5 :rtype: bool 6 """ 7 length = 阅读全文
posted @ 2025-11-14 22:19 Annetree 阅读(4) 评论(0) 推荐(0)
 
摘要: 链接:240. 搜索二维矩阵 II - 力扣(LeetCode) 从右上角开始搜索,即(x,y)从(0,n-1开始),如果[x,y]值小于目标值,x+=1,如果大于目标值,y-=1 1 class Solution(object): 2 def searchMatrix(self, matrix, 阅读全文
posted @ 2025-11-14 14:48 Annetree 阅读(4) 评论(0) 推荐(0)

2025年11月10日

摘要: 链接:48. 旋转图像 - 力扣(LeetCode) 解题思路:找到原始点、最终点和中心点之间的关系,以3*3的矩阵为例 原始点 最终点 中心点 原始点-中心点 最终点-中心点 (1,0) (0,1) (1,1) (0,-1) (-1,0) (2,0) (0,0) (1,1) (1,-1) (-1, 阅读全文
posted @ 2025-11-10 20:35 Annetree 阅读(4) 评论(0) 推荐(0)
 
摘要: 链接:54. 螺旋矩阵 - 力扣(LeetCode) 关键是每次旋转方向都是顺时针,就可以做一个顺时针方向的模拟数组,来模拟下一步的路径 direction = [[0, 1], [1, 0], [0, -1], [-1, 0]] 要变化方向了就把下标+1%4 1 class Solution(ob 阅读全文
posted @ 2025-11-10 19:50 Annetree 阅读(5) 评论(0) 推荐(0)
 
摘要: 链接:73. 矩阵置零 - 力扣(LeetCode) 无脑暴力写 1 class Solution(object): 2 def setZeroes(self, matrix): 3 """ 4 :type matrix: List[List[int]] 5 :rtype: None Do not 阅读全文
posted @ 2025-11-10 17:21 Annetree 阅读(6) 评论(0) 推荐(0)