回溯⚓︎
回溯题的共同点是“枚举选择 + 及时撤销 + 剪枝”。Hot100 里的回溯题主要分成排列与子集、组合生成、网格搜索和约束搜索四类。
排列与子集⚓︎
46. 全排列⚓︎
给定一个不含重复数字的整数数组 nums,请返回它的所有全排列。每个排列都应包含数组中的全部元素,且元素顺序不同就视为不同结果。
输入是整数数组,输出是所有排列组成的列表。题目要求枚举出全部可能的顺序组合,因此结果数量会随元素个数阶乘增长。
排列问题的状态表示是“前面已经固定了哪些位置”。用交换把当前位置与后面的候选值互换,就能在原数组上直接生成每一种排列,回溯时再交换回来恢复现场。
46. 全排列
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
private:
void dfs(int start, vector<int> &nums, vector<vector<int>> &ans) {
if (start == nums.size()) {
ans.push_back(nums);
return;
}
for (int i = start; i < nums.size(); ++i) {
swap(nums[start], nums[i]);
dfs(start + 1, nums, ans);
swap(nums[start], nums[i]);
}
}
public:
vector<vector<int>> permute(vector<int> &nums) {
vector<vector<int>> ans;
dfs(0, nums, ans);
return ans;
}
};
78. 子集⚓︎
给定一个不含重复元素的整数数组 nums,请返回它的所有子集。子集不要求按固定顺序输出,只要覆盖所有可能的选取方案即可。
输入是一个整数数组,输出是包含所有子集的列表。每个元素都只有“选”或“不选”两种状态,因此结果对应整棵二叉决策树的所有叶子与中间状态。
子集问题不要求顺序,所以每个位置只有“选”或“不选”两种分支。递归树的叶子对应完整答案,整棵搜索树本身就是所有子集的覆盖。
78. 子集
#include <vector>
using namespace std;
class Solution {
private:
void dfs(int idx, vector<int> &nums, vector<int> &path, vector<vector<int>> &ans) {
if (idx == nums.size()) {
ans.push_back(path);
return;
}
dfs(idx + 1, nums, path, ans);
path.push_back(nums[idx]);
dfs(idx + 1, nums, path, ans);
path.pop_back();
}
public:
vector<vector<int>> subsets(vector<int> &nums) {
vector<vector<int>> ans;
vector<int> path;
dfs(0, nums, path, ans);
return ans;
}
};
组合生成⚓︎
17. 电话号码的字母组合⚓︎
给定一个只包含数字 2 到 9 的字符串,请返回它在电话按键映射下能够表示的所有字母组合。每个数字对应一组字符,组合结果需要覆盖所有按位选择的可能性。
输入是数字字符串,输出是由若干字母组成的字符串列表。若输入为空,则应返回空结果。
这题就是按位展开的笛卡尔积。每个数字对应一个字符集合,回溯时只需要把当前位所有可选字符依次接到路径后面。
17. 电话号码的字母组合
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
const vector<string> mapping = {
"", "", "abc", "def", "ghi",
"jkl", "mno", "pqrs", "tuv", "wxyz"};
void dfs(const string &digits, int idx, string &path, vector<string> &ans) {
if (idx == digits.size()) {
ans.push_back(path);
return;
}
for (char ch : mapping[digits[idx] - '0']) {
path.push_back(ch);
dfs(digits, idx + 1, path, ans);
path.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> ans;
if (digits.empty()) return ans;
string path;
dfs(digits, 0, path, ans);
return ans;
}
};
39. 组合总和⚓︎
给定一个无重复元素的候选数组 candidates 和目标值 target,请找出所有和为 target 的组合。候选数字可以重复使用,且同一个组合内部的数字顺序不影响结果。
输入是候选数组和目标值,输出是所有满足条件的组合列表。题目要求枚举所有可行方案,同时避免重复组合被计入多次。
先排序再回溯,剪枝条件就很清楚:一旦当前候选值已经大于剩余目标,就可以停止向后搜索。允许重复使用同一个数,因此下一层递归仍然从当前下标开始。
39. 组合总和
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
private:
void dfs(int start, int target, vector<int> &nums, vector<int> &path, vector<vector<int>> &ans) {
if (target == 0) {
ans.push_back(path);
return;
}
for (int i = start; i < nums.size(); ++i) {
if (nums[i] > target) break;
path.push_back(nums[i]);
dfs(i, target - nums[i], nums, path, ans);
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> path;
dfs(0, target, candidates, path, ans);
return ans;
}
};
22. 括号生成⚓︎
给定整数 n,请生成所有由 n 对括号组成的合法括号串。合法括号串要求任意前缀中左括号数量都不少于右括号数量,且最终左右括号数量相等。
输入是 n,输出是所有满足条件的字符串列表。题目关注的是完整枚举而不是单个构造结果,因此搜索过程需要同时维护左右括号的使用数量。
合法性可以转成一个前缀不变量:任意时刻已经使用的右括号数不能超过左括号数。只要同时维护 open 和 close 的数量,搜索过程中就不会产生非法前缀。
22. 括号生成
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
void dfs(int open, int close, int n, string &path, vector<string> &ans) {
if (path.size() == static_cast<size_t>(2 * n)) {
ans.push_back(path);
return;
}
if (open < n) {
path.push_back('(');
dfs(open + 1, close, n, path, ans);
path.pop_back();
}
if (close < open) {
path.push_back(')');
dfs(open, close + 1, n, path, ans);
path.pop_back();
}
}
public:
vector<string> generateParenthesis(int n) {
vector<string> ans;
string path;
dfs(0, 0, n, path, ans);
return ans;
}
};
网格搜索⚓︎
79. 单词搜索⚓︎
给定一个二维字符网格和一个单词 word,请判断该单词是否可以由网格中相邻格子中的字母按顺序拼出。相邻只允许上下左右四个方向,且同一个格子不能在同一次路径中重复使用。
输入是字符网格和目标单词,输出是布尔值。题目要求沿网格做路径搜索,因此需要同时满足字符匹配和访问约束。
这题是标准的带回溯标记的 DFS。每一步只能走上下左右四个方向,并且同一个格子不能重复使用,所以搜索前要暂时标记,搜索结束后再恢复原值。
79. 单词搜索
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
bool dfs(vector<vector<char>> &board, const string &word, int idx, int x, int y) {
if (idx == word.size()) return true;
int m = board.size();
int n = board[0].size();
if (x < 0 || x >= m || y < 0 || y >= n || board[x][y] != word[idx]) return false;
char saved = board[x][y];
board[x][y] = '#';
bool found = dfs(board, word, idx + 1, x + 1, y) ||
dfs(board, word, idx + 1, x - 1, y) ||
dfs(board, word, idx + 1, x, y + 1) ||
dfs(board, word, idx + 1, x, y - 1);
board[x][y] = saved;
return found;
}
public:
bool exist(vector<vector<char>> &board, string word) {
int m = board.size();
int n = board[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (dfs(board, word, 0, i, j)) return true;
}
}
return false;
}
};
约束搜索⚓︎
131. 分割回文串⚓︎
给定一个字符串 s,请将其分割成若干个子串,使得每个子串都是回文串,并返回所有可能的分割方案。每种方案都要完整覆盖整个字符串,不能遗漏也不能重叠。
输入是字符串,输出是所有满足条件的分割列表。题目本质上是在所有切分位置上做回溯枚举,并在每一步检查当前片段是否为回文。
搜索时维护的是切分位置,而不是最终答案本身。每次尝试一个新的结束位置前,先判断子串是否回文,能够把明显无效的分支提前剪掉。
131. 分割回文串
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
bool isPalindrome(const string &s, int l, int r) {
while (l < r) {
if (s[l++] != s[r--]) return false;
}
return true;
}
void dfs(int start, const string &s, vector<string> &path, vector<vector<string>> &ans) {
if (start == s.size()) {
ans.push_back(path);
return;
}
for (int end = start; end < s.size(); ++end) {
if (!isPalindrome(s, start, end)) continue;
path.push_back(s.substr(start, end - start + 1));
dfs(end + 1, s, path, ans);
path.pop_back();
}
}
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> ans;
vector<string> path;
dfs(0, s, path, ans);
return ans;
}
};
51. N 皇后⚓︎
给定一个整数 n,请在 n x n 的棋盘上放置 n 个皇后,使任意两个皇后都不能互相攻击。皇后可以攻击同一行、同一列以及两条对角线上的所有位置。
输入是棋盘大小,输出是所有合法的棋盘摆法。题目要求枚举全部可行解,因此需要在搜索过程中持续维护列和对角线的冲突状态。
这题的剪枝条件是列和两条对角线不能重复占用。行按顺序递归时,每一行只需要尝试当前还没被冲突约束占用的列,搜索空间就会被大幅压缩。
51. N 皇后
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
void dfs(int row, int n, vector<int> &pos, vector<bool> &col, vector<bool> &diag1,
vector<bool> &diag2, vector<vector<string>> &ans) {
if (row == n) {
vector<string> board(n, string(n, '.'));
for (int i = 0; i < n; ++i) board[i][pos[i]] = 'Q';
ans.push_back(board);
return;
}
for (int c = 0; c < n; ++c) {
int d1 = row + c;
int d2 = row - c + n - 1;
if (col[c] || diag1[d1] || diag2[d2]) continue;
pos[row] = c;
col[c] = diag1[d1] = diag2[d2] = true;
dfs(row + 1, n, pos, col, diag1, diag2, ans);
col[c] = diag1[d1] = diag2[d2] = false;
}
}
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> ans;
vector<int> pos(n, -1);
vector<bool> col(n, false), diag1(2 * n - 1, false), diag2(2 * n - 1, false);
dfs(0, n, pos, col, diag1, diag2, ans);
return ans;
}
};