数据结构与算法面试必刷题:从入门到精通 基础数据结构 数组与链表 两数之和 合并K个有序链表 栈与队列 有效括号 哈希表 LRU缓存 树与图 二叉树 二叉树的中序遍历 二叉树的层序遍历 图论 岛屿数量 算法思想 双指针 三数之和 动态规划 爬楼梯 最长递增子序列 回溯 全排列 刷题策略 按难度 Easy:熟悉数据结构API Medium:掌握算法思想 Hard:综合运用+优化 按类型 数组:双指针、滑动窗口 链表:快慢指针、dummy节点 树:递归、迭代、层序 图:DFS、BFS、并查集 DP:线性DP、区间DP 时间安排 第一轮:按类型刷,每道题理解透彻 第二轮:按难度刷,提高熟练度 第三轮:限时模拟,面试节奏 面试技巧 沟通:先说思路,再写代码 边界:考虑空值、单元素
两数之和
def twoSum(nums, target): hashmap = {} for i, num in enumerate(nums): complement = target - num if complement in hashmap: return [hashmap[complement], i] hashmap[num] = i
合并K个有序链表
import heapq def mergeKLists(lists): heap = [] for i, lst in enumerate(lists): if lst: heapq.heappush(heap, (lst.val, i, lst)) dummy = ListNode(0) curr = dummy while heap: val, i, node = heapq.heappop(heap) curr.next = node curr = curr.next if node.next: heapq.heappush(heap, (node.next.val, i, node.next)) return dummy.next
有效括号
def isValid(s): stack = [] mapping = {')': '(', '}': '{', ']': '['} for char in s: if char in mapping: if not stack or stack.pop() != mapping[char]: return False else: stack.append(char) return not stack
LRU缓存
from collections import OrderedDict class LRUCache: def __init__(self, capacity): self.cache = OrderedDict() self.capacity = capacity def get(self, key): if key not in self.cache: return -1 self.cache.move_to_end(key) return self.cache[key] def put(self, key, value): if key in self.cache: self.cache.move_to_end(key) self.cache[key] = value if len(self.cache) > self.capacity: self.cache.popitem(last=False)
二叉树的中序遍历
def inorderTraversal(root): result = [] stack = [] current = root while current or stack: while current: stack.append(current) current = current.left current = stack.pop() result.append(current.val) current = current.right return result
二叉树的层序遍历
from collections import deque def levelOrder(root): if not root: return [] result = [] queue = deque([root]) while queue: level = [] for _ in range(len(queue)): node = queue.popleft() level.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) result.append(level) return result
岛屿数量
def numIslands(grid): if not grid: return 0 count = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == '1': self.dfs(grid, i, j) count += 1 return count def dfs(self, grid, i, j): if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != '1': return grid[i][j] = '0' self.dfs(grid, i+1, j) self.dfs(grid, i-1, j) self.dfs(grid, i, j+1) self.dfs(grid, i, j-1)
三数之和
def threeSum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i-1]: continue left, right = i + 1, len(nums) - 1 while left < right: s = nums[i] + nums[left] + nums[right] if s == 0: result.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left+1]: left += 1 while left < right and nums[right] == nums[right-1]: right -= 1 left += 1 right -= 1 elif s < 0: left += 1 else: right -= 1 return result
爬楼梯
def climbStairs(n): if n <= 2: return n dp = [0] * (n + 1) dp[1] = 1 dp[2] = 2 for i in range(3, n + 1): dp[i] = dp[i-1] + dp[i-2] return dp[n]
最长递增子序列
def lengthOfLIS(nums): if not nums: return 0 dp = [1] * len(nums) for i in range(1, len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) return max(dp)
全排列
def permute(nums): result = [] def backtrack(path, remaining): if not remaining: result.append(path[:]) return for i in range(len(remaining)): path.append(remaining[i]) backtrack(path, remaining[:i] + remaining[i+1:]) path.pop() backtrack([], nums) return result