题库chevron_right回溯·pruning

回溯·pruning 模式

52 道题目

模式页适合用来建立可复用解题框架。先识别题目特征,再按固定流程解释状态定义、转移和边界。

识别信号

  • Do you handle empty input correctly to avoid unnecessary recursion?
  • Can you explain how pruning prevents generating invalid or partial combinations?
  • Checks if you handle repeated use of candidates correctly.

解题流程

  1. 1. 明确窗口/状态定义
  2. 2. 更新状态并维护约束
  3. 3. 用边界样例验证

常见失分点

  • Failing to prune paths when the current combination exceeds the input length, leading to unnecessary recursion.
  • Failing to prune when the path sum exceeds the target, causing unnecessary recursion.
  • Not sorting candidates first, which complicates duplicate skipping.

推荐题单梯度

#题目难度
17

电话号码的字母组合

Generate all letter combinations a digit string can represent using backtracking with pruning, leveraging hash table map…

中等
39

组合总和

Find all unique combinations of numbers from a distinct array that sum to a target using controlled backtracking search.

中等
40

组合总和 II

Find all unique combinations of numbers that sum to a target using backtracking with careful pruning to avoid duplicates…

中等
46

全排列

Generate all possible orderings of a distinct integer array using backtracking search with careful pruning to avoid dupl…

中等
47

全排列 II

Generate all unique permutations of an array containing duplicates using backtracking and pruning to avoid repeated sequ…

中等
51

N 皇后

Solve the N-Queens problem using backtracking with pruning, exploring all valid board placements while avoiding conflict…

困难
52

N 皇后 II

Solve the N-Queens II problem using backtracking with pruning to efficiently count all valid placements for n queens on …

困难
77

组合

Generate all combinations of k numbers from the range [1, n] using backtracking and pruning.

中等
78

子集

Generate all subsets of a set of unique integers using backtracking with pruning to avoid duplicates.

中等
79

单词搜索

Solve Word Search with backtracking by exploring adjacent cells to match a target word in a grid.

中等
89

格雷编码

Generate an n-bit Gray Code sequence using backtracking with pruning and bit manipulation techniques.

中等
90

子集 II

Subsets II problem asks to generate unique subsets from an array with possible duplicates using backtracking search with…

中等
93

复原 IP 地址

Restore all valid IP addresses from a string using backtracking with pruning, avoiding invalid segments or leading zeros…

中等
126

单词接龙 II

Find all shortest transformation sequences from beginWord to endWord using a dictionary, leveraging backtracking search …

困难
212

单词搜索 II

Solve the Word Search II problem using backtracking with pruning to find all words on a board of characters.

困难
216

组合总和 III

Find all unique combinations of k numbers adding to n using efficient backtracking with pruning in arrays.

中等
282

给表达式添加运算符

Expression Add Operators is solved with backtracking that builds multi-digit operands and tracks multiplication preceden…

困难
301

删除无效的括号

Remove the minimum number of invalid parentheses to generate all possible valid strings efficiently using backtracking a…

困难
306

累加数

Additive Number is solved by trying the first two splits, then pruning aggressively while verifying each required sum in…

中等
401

二进制手表

Binary Watch involves generating possible times on a binary watch given a number of turned-on LEDs.

简单
679

24 点游戏

Solve the 24 Game by arranging four card numbers using arithmetic operators and parentheses to reach exactly 24 efficien…

困难
784

字母大小写全排列

Letter Case Permutation involves generating all possible strings by changing the case of each letter in a given string.

中等
816

模糊坐标

Find all valid 2D coordinate possibilities for an ambiguous input string using backtracking and pruning techniques.

中等
842

将数组拆分成斐波那契序列

This problem challenges you to split a string into a Fibonacci-like sequence using backtracking and pruning.

中等
949

给定数字能组成的最大时间

Given four digits, determine the latest valid 24-hour time possible using each digit exactly once with backtracking.

中等
967

连续差相同的数字

Generate all n-digit numbers where consecutive digits differ by k using efficient backtracking and BFS techniques.

中等
980

不同路径 III

Solve the Unique Paths III problem using backtracking search with pruning to count 4-directional paths covering all empt…

困难
1079

活字印刷

Compute all unique non-empty sequences from given letter tiles using backtracking search with pruning efficiently.

中等
1219

黄金矿工

Find the path with the maximum gold in a grid with backtracking and pruning techniques to maximize the gold collected.

中等
1238

循环码排列

Generate a circular permutation from a range of numbers using backtracking and bit manipulation.

中等
1239

串联字符串的最大长度

Find the maximum length of a concatenated string from an array with all unique characters using backtracking search with…

中等
1240

铺瓷砖

Tiling a Rectangle with the Fewest Squares problem asks for the minimum number of squares required to cover a rectangle …

困难
1286

字母组合迭代器

Implement an iterator that generates all combinations of a given length using efficient backtracking with pruning.

中等
1307

口算难题

Check if a verbal arithmetic equation can be solved using a valid digit-letter mapping.

困难
1415

长度为 n 的开心字符串中字典序第 k 小的字符串

Given n and k, find the k-th lexicographical happy string of length n using backtracking search with pruning.

中等
1593

拆分字符串使唯一子字符串的数目最大

Maximize unique substrings in a string using backtracking with pruning and hash tables to track substrings.

中等
1601

最多可达成的换楼请求数目

Find the maximum number of achievable transfer requests between buildings with constraints on net employee transfers.

困难
1718

构建字典序最大的可行序列

Construct the Lexicographically Largest Valid Sequence problem involves finding the largest sequence with backtracking a…

中等
1849

将字符串拆分为递减的连续值

Check if a string of digits can be split into descending consecutive values where the difference between them is 1.

中等
1863

找出所有子集的异或总和再求和

Compute the sum of all subset XOR totals using a backtracking search with pruning for arrays of small size.

简单
2014

重复 K 次的最长子序列

Find the longest subsequence repeated k times in a string using backtracking search with pruning.

困难
2044

统计按位或能得到最大值的子集数目

Determine the number of non-empty subsets that achieve the maximum bitwise OR using efficient backtracking and pruning s…

中等
2048

下一个更大的数值平衡数

Find the smallest numerically balanced number greater than a given integer using backtracking search and pruning.

中等
2056

棋盘上有效移动组合的数目

Given a set of pieces on a chessboard, calculate the number of valid move combinations without overlap using backtrackin…

困难
2065

最大化一张图中的路径价值

Calculate the maximum path quality in a graph using backtracking and pruning to optimize node visits within time limits …

困难
2151

基于陈述统计最多好人数

Determine the maximum number of good people in a group given mutual statements, using precise backtracking with pruning.

困难
2178

拆分成最多数目的正偶数之和

Determine the largest set of unique positive even integers that sum to a given finalSum using backtracking and greedy se…

中等
2212

射箭比赛中的最大得分

In the "Maximum Points in an Archery Competition" problem, Bob aims to maximize his score while respecting Alice's arrow…

中等
2397

被列覆盖的最多行数

Select exactly numSelect columns from a binary matrix to maximize rows where all ones are covered efficiently using back…

中等
2698

求一个整数的惩罚数

Find the punishment number of a given integer using backtracking search with pruning.

中等
3211

生成不含相邻零的二进制字符串

Generate all binary strings of length n without adjacent zeros using backtracking search with pruning for efficient enum…

中等
3348

最小可整除数位乘积 II

Find the smallest zero-free number at least as large as num whose digits multiply to a product divisible by t using care…

困难

关联题型

LeetCode 回溯·pruning模式题解:52题训练路线