LeetCode刷题指南
第 0 章 hot100
0.1 哈希
0.2 双指针
0.3 滑动窗口
0.4 子串
0.5 普通数组
0.6 矩阵
0.7 链表
0.8 二叉树
0.9 图论
0.10 回溯
0.11 二分查找
0.12 栈
0.13 堆
0.14 贪心算法
0.15 动态规划
0.16 多维动态规划
0.17 技巧
第0-1章 面试经典150
0.1 数组/字符串
0.2 双指针
0.3 滑动窗口
链表
二叉树
第 1 章 最易懂的贪心算法
1.1 算法解释
1.2 分配问题
1.3 区间问题
1.4 练习
第 2 章 玩转双指针
2.1 算法解释
2.2 Two Sum
2.3 归并两个有序数组
2.4 滑动窗口
2.5 快慢指针
2.6 练习
第 3 章 居合斩!二分查找
3.1 算法解释
3.2 求开方
3.3 查找区间
3.4 查找峰值
3.5 旋转数组查找数字
3.6 练习
第 4 章 千奇百怪的排序算法
4.1 常用排序算法
4.2 快速选择
4.3 桶排序
4.4 练习
第 5 章 一切皆可搜索
5.1 算法解释
5.2 深度优先搜索
5.3 回溯法
5.4 广度优先搜索
5.5 练习
第 6 章 深入浅出动态规划
6.1 算法解释
6.2 基本动态规划:一维
6.3 基本动态规划:二维
6.4 分割类型题
6.5 子序列问题
6.6 背包问题
6.7 字符串编辑
6.8 股票交易
6.9 练习
第 7 章 化繁为简的分治法
7.1 算法解释
7.2 表达式问题
7.3 练习
第 8 章 巧解数学问题
8.1 引言
8.2 公倍数与公因数
8.3 质数
8.4 数字处理
8.5 随机与取样
8.6 练习
第 9 章 神奇的位运算
9.1 常用技巧
9.2 位运算基础问题
9.3 二进制特性
9.4 练习
第 10 章 妙用数据结构
10.1 C++ STL
10.2 Python 常用数据结构
10.3 数组
10.4 栈和队列
10.5 单调栈
10.6 优先队列
10.7 双端队列
10.8 哈希表
10.9 多重集合和映射
10.10 前缀和与积分图
10.11 练习
第 11 章 令人头大的字符串
11.1 引言
11.2 字符串比较
11.3 字符串理解
11.4 字符串匹配
11.5 练习
第 12 章 指针三剑客之一:链表
12.1 数据结构介绍
12.2 链表的基本操作
12.3 其它链表技巧
12.4 练习
第 13 章 指针三剑客之二:树
13.1 数据结构介绍
13.2 树的递归
13.3 层次遍历
13.4 前中后序遍历
13.5 二叉查找树
13.6 字典树
13.7 练习
第 14 章 指针三剑客之三:图
14.1 数据结构介绍
14.2 二分图
14.3 拓扑排序
14.4 练习
第 15 章 更加复杂的数据结构
15.1 引言
15.2 并查集
15.3 复合数据结构
15.4 练习
第16章 面试题
第 17 章 十大经典排序算法
README
本文档使用 MrDoc 发布
-
+
首页
0.1 数组/字符串
[88. 合并两个有序数组](https://leetcode.cn/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ i,j,k = m-1,n-1,m+n-1 while j >= 0: if i >= 0 and nums1[i] > nums2[j]: nums1[k] = nums1[i] i -= 1 else: nums1[k] = nums2[j] j -= 1 k -= 1 ``` [27. 移除元素](https://leetcode.cn/problems/remove-element/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def removeElement(self, nums: List[int], val: int) -> int: n = len(nums) i = j = 0 while j < n: if nums[j] != val: nums[i] = nums[j] i += 1 j += 1 else: j += 1 return i ``` [26. 删除有序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def removeDuplicates(self, nums: List[int]) -> int: n = len(nums) s = 0 for f in range(1,n): if nums[f] != nums[s]: s += 1 nums[s] = nums[f] return s + 1 ``` [80. 删除有序数组中的重复项 II](https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def removeDuplicates(self, nums: List[int]) -> int: n = len(nums) i = 1 for j in range(2,n): if nums[j] != nums[i] or nums[j] != nums[i-1]: i += 1 nums[i] = nums[j] return i + 1 ``` [169. 多数元素](https://leetcode.cn/problems/majority-element/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def majorityElement(self, nums: List[int]) -> int: n = len(nums) count = 0 for i in range(n): if count == 0: candidate = nums[i] count += 1 else: if candidate == nums[i]: count += 1 else: count -= 1 return candidate ``` [189. 轮转数组](https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ def revert(nums, left, right): while left <= right: nums[left], nums[right] = nums[right], nums[left] left += 1 right -= 1 n = len(nums) k = k % n revert(nums, 0, n-1) revert(nums, 0, k-1) revert(nums, k, n-1) ``` [121. 买卖股票的最佳时机](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def maxProfit(self, prices: List[int]) -> int: buy = -inf sell = 0 for p in prices: buy = max(buy, -p) sell = max(sell, p + buy) return sell ``` [122. 买卖股票的最佳时机 II](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def maxProfit(self, prices: List[int]) -> int: res = 0 n = len(prices) if n <= 1: return 0 for i in range(1, n): if prices[i] > prices[i - 1]: res += prices[i] - prices[i - 1] return res ``` [55. 跳跃游戏](https://leetcode.cn/problems/jump-game/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def canJump(self, nums: List[int]) -> bool: far = 0 n = len(nums) for i in range(n): if i > far: return False far = max(far, i + nums[i]) if far >= n - 1: return True ``` [45. 跳跃游戏 II](https://leetcode.cn/problems/jump-game-ii/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def jump(self, nums: List[int]) -> int: cur_far = 0 far = 0 n = len(nums) res = 0 for i in range(n): if cur_far >= n - 1: return res far = max(far, nums[i] + i) if i == cur_far: res += 1 cur_far = far ``` [274. H 指数](https://leetcode.cn/problems/h-index/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def hIndex(self, citations: List[int]) -> int: citations.sort(reverse=True) for i in range(len(citations)): # 第 i+1篇论文的引用次数是否小于 i+1。 if citations[i] < i + 1: return i return len(citations) ``` [380. O(1) 时间插入、删除和获取随机元素](https://leetcode.cn/problems/insert-delete-getrandom-o1/submissions/658076908/?envType=study-plan-v2&envId=top-interview-150) ```python class RandomizedSet: def __init__(self): self.nums = [] self.nums_idx = {} def insert(self, val: int) -> bool: if val in self.nums_idx: return False self.nums_idx[val] = len(self.nums) self.nums.append(val) return True def remove(self, val: int) -> bool: if val not in self.nums_idx: return False remove_idx = self.nums_idx[val] last_num = self.nums[-1] self.nums[remove_idx] = last_num self.nums_idx[last_num] = remove_idx self.nums.pop() del self.nums_idx[val] return True def getRandom(self) -> int: return random.choice(self.nums) ``` [238. 除自身以外数组的乘积](https://leetcode.cn/problems/product-of-array-except-self/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: n = len(nums) res = [1] * n for i in range(1, n): res[i] = res[i - 1] * nums[i - 1] temp = 1 for i in range(n - 2, -1, -1): temp *= nums[i + 1] res[i] = res[i] * temp return res ``` [134. 加油站](https://leetcode.cn/problems/gas-station/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: if sum(gas) < sum(cost): return -1 total = 0 start = 0 for i in range(len(gas)): total += gas[i] - cost[i] if total < 0: start = i + 1 total = 0 return start ``` [135. 分发糖果](https://leetcode.cn/problems/candy/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def candy(self, ratings: List[int]) -> int: n = len(ratings) res = [1] * n for i in range(1,n): if ratings[i] > ratings[i-1]: res[i] = res[i-1] + 1 for i in range(n-2,-1,-1): if ratings[i] > ratings[i+1]: res[i] = max(res[i], res[i+1] + 1) return sum(res) ``` [42. 接雨水](https://leetcode.cn/problems/trapping-rain-water/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def trap(self, height: List[int]) -> int: n = len(height) left, right = 0, n - 1 max_left = max_right = 0 res = 0 while left < right: max_left = max(max_left, height[left]) max_right = max(max_right, height[right]) res += max_left - height[left] + max_right - height[right] if height[left] < height[right]: left += 1 else: right -= 1 return res ``` [13. 罗马数字转整数](https://leetcode.cn/problems/roman-to-integer/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def romanToInt(self, s: str) -> int: roman_int = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} res = 0 prev = 0 for c in s: cur = roman_int[c] if cur > prev: res += cur - 2 * prev else: res += cur prev = cur return res ``` [12. 整数转罗马数字](https://leetcode.cn/problems/integer-to-roman/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def intToRoman(self, num: int) -> str: nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] val = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] string = "" for i in range(len(nums)): while num >= nums[i]: num -= nums[i] string += val[i] return string ``` [58. 最后一个单词的长度](https://leetcode.cn/problems/length-of-last-word/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def lengthOfLastWord(self, s: str) -> int: n = len(s) right = n-1 while right >= 0 and s[right] == ' ': right -= 1 left = right while left >= 0 and s[left] != ' ': left -= 1 return right - left ``` [14. 最长公共前缀](https://leetcode.cn/problems/longest-common-prefix/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: s0 = strs[0] for j, c in enumerate(s0): for s in strs: if j == len(s) or s[j] != c: return s0[:j] return s0 ``` [151. 反转字符串中的单词](https://leetcode.cn/problems/reverse-words-in-a-string/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def reverseWords(self, s: str) -> str: s = s.strip().split() s.reverse() return ' '.join(s) ``` [6. Z 字形变换](https://leetcode.cn/problems/zigzag-conversion/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def convert(self, s: str, numRows: int) -> str: if numRows < 2: return s res = ["" for _ in range(numRows)] i = 0 flag = -1 for c in s: res[i] += c if i == 0 or i == numRows - 1: flag = -flag i += flag return "".join(res) ``` [28. 找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/description/?envType=study-plan-v2&envId=top-interview-150) ```python class Solution: def strStr(self, haystack: str, needle: str) -> int: if not needle: return 0 m, n = len(haystack), len(needle) for i in range(m - n + 1): if haystack[i:i+n] == needle: return i return -1 ``` []() ```python ``` []() ```python ``` []() ```python ```
嘉心糖糖
2025年8月31日 16:37
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码