自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(224)
  • 收藏
  • 关注

原创 【力扣435. 无重叠区间】多种解法:贪心、dp(python3)

目录题目描述思路题解方法1:贪心方法2:dp题目描述https://leetcode-cn.com/problems/non-overlapping-intervals/思路题解方法1:贪心本题可以转化为求不相交的区间个数。贪心算法:1. 按照区间的右端点从小到大排序2. 每次都选择右端点最小的,不会出现重叠的区间,因为这样可以为后侧留下更多的选择空间自己做的代码:class Solution: def eraseOverlapIntervals(self, intervals

2021-09-14 20:10:57 395

原创 动态规划总结

目录1. 0-1背包1.1 相关链接1.2 问题描述1.3 解决实现1. 0-1背包1.1 相关链接python解决0-1背包问题(超直观)01背包问题1.2 问题描述有n件物品,每件物品的重量为w[i],价值为c[i]。现有一个容量为V的背包,问如何选取物品放入背包,使得背包内物品的总价值最大。其中每种物品都只有一件。1.3 解决实现令dp[i][j]来表示前i件物品装入容量为j的背包所能得到的最大总价值。对于dp[i][j]来说,i指的是前i件物品,j指的是还剩下多少背包空间。于

2021-08-30 21:36:25 309

原创 【力扣28. 实现 strStr()】KMP算法(Python3)

题目描述https://leetcode-cn.com/problems/implement-strstr/思路题解运用KMP算法class Solution: def strStr(self, haystack: str, needle: str) -> int: def getNext(p): n=len(p) next=[-1]*n j,k=0,-1 while j&

2021-08-24 15:27:38 331 1

原创 【力扣41. 缺失的第一个正数】仿哈希表法、置换法(Python3)

目录题目描述思路题解方法一:仿哈希表方法二:置换法题目描述https://leetcode-cn.com/problems/first-missing-positive/思路题解https://leetcode-cn.com/problems/first-missing-positive/solution/que-shi-de-di-yi-ge-zheng-shu-by-leetcode-solution/方法一:仿哈希表时间复杂度 :O(3N)=O(N)空间复杂度:O(1)class

2021-08-24 10:51:10 167

原创 【力扣1392. 最长快乐前缀】前缀后缀数组、Rabin-Karp 字符串编码、kmp(Python3)

题目描述https://leetcode-cn.com/problems/longest-happy-prefix/思路题解方法一:前缀后缀数组不推荐class Solution: def longestPrefix(self, s: str) -> str: front,end={},{} a,b,n="","",len(s) ans="" for i in range(n-1): a=a+

2021-08-23 20:53:57 188

原创 【力扣1044. 最长重复子串】二分查找+Rabin-Karp 字符串编码、后缀数组(Python3)

题目描述https://leetcode-cn.com/problems/longest-duplicate-substring/思路题解方法一:二分查找+Rabin-Karp 字符串编码https://leetcode-cn.com/problems/longest-duplicate-substring/solution/zui-chang-zhong-fu-zi-chuan-by-leetcode/class Solution: def longestDupSubstring(

2021-08-23 19:14:14 194

原创 【力扣16. 最接近的三数之和】排序&双指针(Python3)

题目描述https://leetcode-cn.com/problems/3sum-closest/思路题解https://leetcode-cn.com/problems/3sum-closest/solution/zui-jie-jin-de-san-shu-zhi-he-by-leetcode-solution/class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int:

2021-08-23 15:46:25 90

原创 【力扣856. 括号的分数】分治+栈(Python3)

题目描述https://leetcode-cn.com/problems/score-of-parentheses/思路题解https://leetcode-cn.com/problems/score-of-parentheses/solution/gua-hao-de-fen-shu-by-leetcode/方法1:分治法class Solution(object): def scoreOfParentheses(self, S): def F(i, j):

2021-08-22 17:13:14 85

原创 美团2022秋招笔试题-小美的数学题-栈

s=input()stack=[]if len(s)==1:print(0)else: for i in range(len(s)): if s[i]=="(": stack.append(0) else: if s[i-1]=="(": stack.pop() stack.append(2) else:

2021-08-22 16:24:04 1903

原创 【京东笔试题】熊猫吃竹子,回溯

题目描述输入n,a,b,c,n代表柱子长度,a,b,c代表可以切割的大小。求这个竹子最多能被分成多少分(不能有剩余)思路题解回溯法def recall(count, base, res,s): #base没有被压入 if count -base == 0: print(s+str(base)+' ') return res+1 if count - base>0: x = recall(count - base, a, r

2021-08-21 22:00:55 456

原创 【力扣402. 移掉 K 位数字】单调栈(Python3)

题目描述https://leetcode-cn.com/problems/remove-k-digits/给你一个以字符串表示的非负整数 num 和一个整数 k ,移除这个数中的 k 位数字,使得剩下的数字最小。请你以字符串形式返回这个最小的数字。示例 1 :输入:num = “1432219”, k = 3输出:“1219”解释:移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219 。示例 2 :输入:num = “10200”, k = 1输出:“200”解释:移掉首

2021-08-20 17:45:37 180

原创 【力扣325. 和等于 k 的最长子数组长度】前缀和(Python3)

题目描述https://leetcode-cn.com/problems/maximum-size-subarray-sum-equals-k/思路题解滑动窗口+剪枝(时间超限)因为本题目有负数,所以对于负数比较多而k为正数的情况,会时间超限。class Solution: def maxSubArrayLen(self, nums: List[int], k: int) -> int: n=len(nums) preSum=[0]*n

2021-08-20 15:30:28 640

原创 【力扣131. 分割回文串】dp(python3)

题目描述https://leetcode-cn.com/problems/palindrome-partitioning/思路题解dp[i]代表s[0~i]的字符串的回文子串是哪些。我们求解的时候每次分成两部分,例如s=aaab:i=0时,dp[0]=[“a”]i=1时,dp[1]=[[“a”,“a”],[“aa”]] (其中[“a”,“a”]为上一步剩下的+s[i],[“aa”]为s[i]逐个往前收揽得到的)以此类推…输入:"aaab"dp数组:[['a']][['a', '

2021-08-15 20:06:55 128

原创 【力扣88. 合并两个有序数组】三指针(python3)

题目描述https://leetcode-cn.com/problems/merge-sorted-array/思路题解使用三个指针i,j,k:i指向nums1存在有效数据的结尾,j指向nums2存在有效数据的结尾,k指向nums1结尾。从后往前遍历,谁大谁放在nums[k]上,最后当一方放完了的时候,在把剩余的存入(只考虑i全放完j没有放完的情况)class Solution: def merge(self, nums1: List[int], m: int, nums2: List[

2021-08-15 16:10:21 135

原创 【力扣-233. 数字 1 的个数】数学法(python3)

题目描述https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/思路题解https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/solution/mian-shi-ti-43-1n-zheng-shu-zhong-1-chu-xian-de-2/根据当前位 curcur 值的不同,分为以下三种情况:case

2021-08-15 15:54:38 153

原创 设计题总结

目录1. 剑指 Offer 59 - II. 队列的最大值2. LRU1. 剑指 Offer 59 - II. 队列的最大值https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。若队列为空,pop_front 和 max_value 需要返回 -1示例 1:输入:

2021-08-13 12:42:48 75

原创 【力扣1143. 最长公共子序列】dp多种解法(python3)

题目描述https://leetcode-cn.com/problems/longest-common-subsequence/思路题解dp[i][j]代表text1前i个数和text2前j个数的最长公共子序列的长度class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m,n=len(text1),len(text2) dp=[[0]*n fo

2021-08-11 20:40:12 148

原创 【力扣40. 组合总和 II】回溯(python3)

题目描述https://leetcode-cn.com/problems/combination-sum-ii/思路题解一开始的错误做法:会卡在相同的小的数字特别多的情况:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]27[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

2021-08-11 16:48:01 133

原创 【力扣470. 用 Rand7() 实现 Rand10()】概率论,拒绝采样(python3)

目录题目描述思路题解1.两次调用的拒绝采样2.调用4次的拒绝采样思考题目描述https://leetcode-cn.com/problems/implement-rand10-using-rand7/思路题解方法1、2题解:https://leetcode-cn.com/problems/implement-rand10-using-rand7/solution/yong-rand7-shi-xian-rand10-by-leetcode/1.两次调用的拒绝采样# The rand7() AP

2021-08-10 21:02:06 205

原创 【力扣76. 最小覆盖子串】滑动窗口(python3)

题目描述https://leetcode-cn.com/problems/minimum-window-substring/思路题解https://leetcode-cn.com/problems/minimum-window-substring/solution/tong-su-qie-xiang-xi-de-miao-shu-hua-dong-chuang-k/注意collections.defaultdict的使用class Solution: def minWindow(self,

2021-08-08 20:46:58 165

原创 【力扣752. 打开转盘锁】双向BFS(python3)

题目描述https://leetcode-cn.com/problems/open-the-lock/思路题解双向bfs https://leetcode-cn.com/problems/open-the-lock/solution/gong-shui-san-xie-yi-ti-shuang-jie-shuan-wyr9/class Solution: def openLock(self, deadends: List[str], target: str) -> int:

2021-08-07 15:51:57 181

原创 【力扣103. 二叉树的锯齿形层序遍历】BFS双端队列(多种解法)(python3)

题目描述https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/思路题解自己写的方法,层序遍历后按层数逆转class Solution: def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]: if not root:return [] from collections import deque

2021-08-05 14:54:22 89

原创 【力扣143. 重排链表】寻找链表中点 + 链表逆序 + 合并链表(python3)

题目描述https://leetcode-cn.com/problems/reorder-list/这个题还能顺便练习寻找链表中点 + 链表逆序思路题解方法二:寻找链表中点 + 链表逆序 + 合并链表注意到目标链表即为将原链表的左半端和反转后的右半端合并后的结果。这样我们的任务即可划分为三步:找到原链表的中点(参考「876. 链表的中间结点」)。我们可以使用快慢指针来 O(N) 地找到链表的中间节点。将原链表的右半端反转(参考「206. 反转链表」)。我们可以使用迭代法实现链表的反转。将

2021-08-05 13:20:36 235

原创 【力扣剑指 Offer 60. n个骰子的点数】dp(python3)

题目描述https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof/思路题解dp二维动态规划,第三轮遍历“和-上一轮的骰子数”。dp递归方程dp[i][j]代表i+1个骰子的和为等于j时候的概率。dp[i][j]=Sum(dp[i-1][j-k])0<=i<=n-1,i+1<=j<=6n,1<=k<=6,并判断j-k>=0时就更新dp.初始条件:dp[0][1:7]=[1/6]*6最

2021-07-31 15:05:35 81

原创 【力扣剑指 Offer 66. 构建乘积数组】前缀和-多种做法(python3)

题目描述https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/思路题解前缀和:class Solution: def constructArr(self, a: List[int]) -> List[int]: if not a:return [] n=len(a) l,r=[0]*n,[0]*n l[0],r[0]=a[0],a[n-1]

2021-07-31 11:04:41 94

原创 【力扣剑指 Offer 49. 丑数、力扣264】dp+最小堆(python3)

题目描述https://leetcode-cn.com/problems/chou-shu-lcof/思路题解https://leetcode-cn.com/problems/chou-shu-lcof/solution/chou-shu-by-leetcode-solution-0e5i/dpclass Solution: def nthUglyNumber(self, n: int) -> int: dp = [0] * (n + 1) dp[1]

2021-07-28 14:33:32 72

原创 旋转数组总结(力扣33 搜索旋转排序数组、81 搜索旋转排序数组 II)

33 搜索旋转排序数组81 搜索旋转排序数组 II

2021-07-28 10:12:21 81

原创 【力扣剑指 Offer 64. 求1+2+…+n】位运算+短路思想(Python3)

题目描述https://leetcode-cn.com/problems/qiu-12n-lcof/思路题解位运算,逐位运算https://leetcode-cn.com/problems/qiu-12n-lcof/solution/qiu-12n-by-leetcode-solution/短路思想https://leetcode-cn.com/submissions/detail/200346446/class Solution: def sumNums(self, n: int)

2021-07-27 17:41:44 77

原创 【力扣剑指 Offer 46. 把数字翻译成字符串】dp(python3)

题目描述https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof/思路题解class Solution: def translateNum(self, num: int) -> int: if num<10:return 1 s=str(num) dp=[0]*len(s) if int(s[0]+s[1])<=25:dp[

2021-07-27 16:15:39 83

原创 【力扣剑指 Offer 56 - I. 数组中数字出现的次数、数组中数字出现的次数 II】位运算+多解法

题目描述https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/思路题解https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/solution/jian-zhi-offer-56-i-shu-zu-zhong-shu-zi-tykom/class Solution: def singleNumbe

2021-07-26 15:42:30 79

原创 【力扣25. K 个一组翻转链表】模拟(python3)

题目描述https://leetcode-cn.com/problems/reverse-nodes-in-k-group/思路题解start start.next…tmp end,其中[start.next,tmp]是需要反转的地方。# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val#

2021-07-23 09:37:01 91

原创 【力扣-剑指 Offer 44. 数字序列中某一位的数字、主站400】模拟(python3)

题目描述https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/思路题解https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/mian-shi-ti-44-shu-zi-xu-lie-zhong-mou-yi-wei-de-6/class Solution: def f

2021-07-19 11:38:28 67

原创 【力扣206. 反转链表】递归+迭代(三指针)(Python3)

题目描述https://leetcode-cn.com/problems/reverse-linked-list/思路题解递归class Solution: def reverseList(self, head: ListNode) -> ListNode: if not head or not head.next:return head p=self.reverseList(head.next) head.next.next=head

2021-07-18 11:18:36 104

原创 【力扣剑指 Offer 36. 二叉搜索树与双向链表、 426 】取头尾拼接的dfs+标记遍历的dfs(python3)

题目描述https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/思路题解取头尾拼接的dfs自己做的首先后续遍历,得到左右子树的链表的头尾节点,然后在把中间节点和他们拼接起来,最后返回新的头尾节点。"""# Definition for a Node.class Node: def __init__(self, val, left=None, right=None):

2021-07-16 17:24:21 316

原创 【力扣剑指 Offer 35. 复杂链表的复制、138. 复制带随机指针的链表】哈希表 +拼接与拆分(python3))

题目描述https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/思路题解https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/solution/jian-zhi-offer-35-fu-za-lian-biao-de-fu-zhi-ha-xi-/哈希表利用哈希表的查询特点,考虑构建 原链表节点 和 新链表对应节点 的键值对映射关系,再遍历构建新链表各节点

2021-07-16 15:17:00 102

原创 【力扣剑指 Offer 20. 表示数值的字符串】有限状态自动机(python3)

题目描述https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/思路题解https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/solution/mian-shi-ti-20-biao-shi-shu-zhi-de-zi-fu-chuan-y-2/class Solution: def isNumber(self, s: s

2021-07-16 13:37:22 130

原创 【力扣剑指 Offer 14- I. 剪绳子、343. 整数拆分、剑指 Offer 14- II. 剪绳子 II】dp(python3)

题目描述https://leetcode-cn.com/problems/integer-break/思路题解dp,其中2、3的要单独考虑,因为2、3单独拆分和正常拆分不一样class Solution: def integerBreak(self, n: int) -> int: if n==2:return 1 if n==3:return 2 dp=[0]*(n+1) dp[2]=2 dp[3]=3

2021-07-14 17:01:04 76

原创 【力扣剑指 Offer 13. 机器人的运动范围】bfs可达性分析(python3)

题目描述https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/思路题解https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/solution/mian-shi-ti-13-ji-qi-ren-de-yun-dong-fan-wei-dfs-b/from collections import dequeclass Solution:

2021-07-13 17:31:52 185

原创 【力扣72. 编辑距离】dp(Python3)

题目描述https://leetcode-cn.com/problems/edit-distance/思路题解https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-by-leetcode-solution/状态拆解为3个状态,进行分析:对“dp[i-1][j-1] 表示替换操作,dp[i-1][j] 表示删除操作,dp[i][j-1] 表示插入操作。”的补充理解:以 word1 为 “horse”,word

2021-07-13 15:45:04 176

原创 树的公共祖先总结(236. 二叉树的最近公共祖先、235. 二叉搜索树的最近公共祖先)

目录236. 二叉树的最近公共祖先235. 二叉搜索树的最近公共祖先递归1递归2迭代236. 二叉树的最近公共祖先236. 二叉树的最近公共祖先class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': def dfs(root): if not root or root==p or roo

2021-07-13 10:44:52 159

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除