class Solution {
private:
vector<vector<int>> result; // 存放符合条件结果的集合
vector<int> path; // 用来存放符合条件结果
void backtracking(int n, int k, int startIndex) {
//当记录数组个数到k时
if(path.size()==k){
result.push_back(path);
return;
}
//startindex代表这次递归的开始位置
for(int i=startIndex;i<=n;i++){
path.push_back(i);//先填入
backtracking(n,k,i+1);//递归判断i+1到n范围内的情况
path.pop_back();//回溯,在以i+1为头判断
}
}
public:
vector<vector<int>> combine(int n, int k) {
backtracking(n, k, 1);
return result;
}
};
class Solution {
public:
vector<vector<int>> result; // 存放结果集
vector<int> path;
void back(int k,int n,int index,int sum){
if(path.size()==k){
//达到条件收
if(sum==n){
result.push_back(path);
}
return;
}
for(int i=index;i<=9;i++){
sum+=i;
path.push_back(i);
back(k,n,i+1,sum);
sum-=i;
path.pop_back();
}
}
vector<vector<int>> combinationSum3(int k, int n) {
back(k,n,1,0);
return result;
}
};
class Solution {
private:
const string letterMap[10] = {
"", // 0
"", // 1
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz", // 9
};
public:
vector<string> result;
string s;
void backtracking(const string& digits, int index) {
if(index==digits.size()){
result.push_back(s);
return;
}
int k=digits[index]-'0';//将要转化的字符串的数字提取出来
for(int i=0;i<letterMap[k].size();i++){//每次从零开始
s.push_back(letterMap[k][i]);
backtracking(digits,index+1);//index++
s.pop_back();
}
}
vector<string> letterCombinations(string digits) {
s.clear();
result.clear();
if (digits.size() == 0) {
return result;
}
backtracking(digits, 0);
return result;
}
};
class Solution {
public:
vector<int>res;
vector<vector<int>>result;
void back(vector<int>& candidates, int target,int sum,int index){
if(sum>target){
return;
}
if(sum==target){
result.push_back(res);
return;
}
//树枝去重
//每一次递归都选取全部进行组合->每一次递归选取当前的一直到最后进行组合
for(int i=index;i<candidates.size();i++){
res.push_back(candidates[i]);
sum+=candidates[i];
back(candidates,target,sum,i);
sum-=candidates[i];
res.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
back(candidates,target,0,0);
return result;
}
};
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
if(sum>target){
return;
}
if(sum==target){
result.push_back(path);
return;
}
for(int i=startIndex;i<candidates.size();i++){
//进入递归的不去重,但这一层要去重
//树层去重,跳过相同节点
if(i>startIndex&&candidates[i]==candidates[i-1]){
continue;
}
path.push_back(candidates[i]);
sum+=candidates[i];
backtracking(candidates,target,sum,i+1);
sum-=candidates[i];
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
path.clear();
result.clear();
// 首先把给candidates排序,让其相同的元素都挨在一起。
sort(candidates.begin(), candidates.end());
backtracking(candidates, target, 0, 0);
return result;
}
};
class Solution {
private:
vector<vector<string>> result;
vector<string> path; // 放已经回文的子串
void backtracking (const string& s, int startIndex) {
if(startIndex==s.size()){
result.push_back(path);
return;
}
for(int i=startIndex;i<s.size();i++){
//如果是回文串
if(isPalindrome(s,startIndex,i)){
string str=s.substr(startIndex,i-startIndex+1);
path.push_back(str);
backtracking(s,i+1);
path.pop_back();
}
else{
continue;
}
}
}
bool isPalindrome(const string& s, int start, int end) {
for (int i = start, j = end; i < j; i++, j--) {
if (s[i] != s[j]) {
return false;
}
}
return true;
}
public:
vector<vector<string>> partition(string s) {
result.clear();
path.clear();
backtracking(s, 0);
return result;
}
};
class Solution {
public:
vector<string>res;
void back(string s,int index,int num){
if(num==3){
if(judge(s,index,s.size()-1)){
res.push_back(s);
}
return;
}
for(int i=index;i<s.size();i++){
if(judge(s,index,i)){
s.insert(s.begin()+i+1,'.');
num+=1;
back(s,i+2,num);
num-=1;
s.erase(s.begin()+i+1);
}
else{
break;
}
}
}
bool judge(string s,int i,int j){
if(s[i]=='0'&&i!=j)return false;
if(i>j)return false;//重要
int num=0;
for(int k=i;k<=j;k++){
if(s[k]>'9'||s[k]<'0'){
return false;
}
num=num*10+(s[k]-'0');
if(num>255)return false;
}
return true;
}
vector<string> restoreIpAddresses(string s) {
back(s,0,0);
return res;
}
};
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& nums, int startIndex) {
result.push_back(path);
if(startIndex==nums.size()){
return;
}
for(int i=startIndex;i<nums.size();i++){
path.push_back(nums[i]);
backtracking(nums,i+1);
path.pop_back();
}
}
public:
vector<vector<int>> subsets(vector<int>& nums) {
result.clear();
path.clear();
backtracking(nums, 0);
return result;
}
};
class Solution {
public:
vector<vector<int>>result;
vector<int>res;
void back(vector<int>&nums,int startindex){
result.push_back(res);
if(startindex==nums.size()){
return;
}
for(int i=startindex;i<nums.size();i++){
//树层去重
if(i>startindex&&nums[i]==nums[i-1]){
continue;
}
res.push_back(nums[i]);
back(nums,i+1);
res.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
back(nums,0);
return result;
}
};
class Solution {
public:
vector<vector<int>>result;
vector<int>path;
void back(vector<int>&nums,int index){
if(path.size()>=2){
result.push_back(path);
}
unordered_set<int>hash;
for(int i=index;i<nums.size();i++){
//树层去重
if((!path.empty()&&path.back()>nums[i])||hash.find(nums[i])!=hash.end()){
continue;
}
hash.insert(nums[i]);
path.push_back(nums[i]);
back(nums,i+1);
path.pop_back();
}
}
vector<vector<int>> findSubsequences(vector<int>& nums) {
back(nums,0);
return result;
}
};
class Solution {
public:
vector<vector<int>>result;
vector<int>path;
void back(vector<int>&nums,vector<bool>&used){
if(path.size()==nums.size()){
result.push_back(path);
return;
}
for(int i=0;i<nums.size();i++){
if(used[i]==false){
used[i]=true;
path.push_back(nums[i]);
back(nums,used);
used[i]=false;//回溯,树枝去重
path.pop_back();
}
else{
continue;
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<bool>used(nums.size(),false);
back(nums,used);
return result;
}
};
class Solution {
public:
vector<vector<int>>result;
vector<int>path;
void back(vector<int>&nums,vector<bool>&used){
if(path.size()==nums.size()){
result.push_back(path);
return;
}
for(int i=0;i<nums.size();i++){
//树层去重
if(i>0&&nums[i]==nums[i-1]&&used[i-1]==false){
continue;
}
if(used[i]==false){//等于true的话在上一层被使用过
used[i]=true;//下一步递归的不能使用了
path.push_back(nums[i]);
back(nums,used);
used[i]=false;//回溯,树枝去重
path.pop_back();
}
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
if(nums.empty())return {};
sort(nums.begin(),nums.end());
vector<bool> used(nums.size(), false);
back(nums,used);
return result;
}
};
class Solution {
private:
// unordered_map<出发机场, map<到达机场, 航班次数>> targets
unordered_map<string, map<string, int>> targets;
bool backtracking(int ticketNum, vector<string>& result) {
if (result.size() == ticketNum + 1) {
return true;
}
for (pair<const string, int>& target : targets[result[result.size() - 1]]) {
if (target.second > 0 ) { // 记录到达机场是否飞过了
result.push_back(target.first);
target.second--;
if (backtracking(ticketNum, result)) return true;
result.pop_back();
target.second++;
}
}
return false;
}
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
targets.clear();
vector<string> result;
for (const vector<string>& vec : tickets) {
targets[vec[0]][vec[1]]++; // 记录映射关系
}
result.push_back("JFK"); // 起始机场
backtracking(tickets.size(), result);
return result;
}
};
class Solution {
private:
vector<vector<string>> result;
// n 为输入的棋盘大小
// row 是当前递归到棋盘的第几行了
void backtracking(int n, int row, vector<string>& chessboard) {
if (row == n) {
result.push_back(chessboard);
return;
}
for (int col = 0; col < n; col++) {
if (isValid(row, col, chessboard, n)) { // 验证合法就可以放
chessboard[row][col] = 'Q'; // 放置皇后
backtracking(n, row + 1, chessboard);
chessboard[row][col] = '.'; // 回溯,撤销皇后
}
}
}
bool isValid(int row, int col, vector<string>& chessboard, int n) {
// 检查列
for (int i = 0; i < row; i++) { // 这是一个剪枝
if (chessboard[i][col] == 'Q') {
return false;
}
}
// 检查 45度角是否有皇后
for (int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--) {
if (chessboard[i][j] == 'Q') {
return false;
}
}
// 检查 135度角是否有皇后
for(int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (chessboard[i][j] == 'Q') {
return false;
}
}
return true;
}
public:
vector<vector<string>> solveNQueens(int n) {
result.clear();
std::vector<std::string> chessboard(n, std::string(n, '.'));
backtracking(n, 0, chessboard);
return result;
}
};
bool backtracking(vector<vector<char>>& board) {
for (int i = 0; i < board.size(); i++) { // 遍历行
for (int j = 0; j < board[0].size(); j++) { // 遍历列
if (board[i][j] != '.') continue;
for (char k = '1'; k <= '9'; k++) { // (i, j) 这个位置放k是否合适
if (isValid(i, j, k, board)) {
board[i][j] = k; // 放置k
if (backtracking(board)) return true; // 如果找到合适一组立刻返回
board[i][j] = '.'; // 回溯,撤销k
}
}
return false; // 9个数都试完了,都不行,那么就返回false
}
}
return true; // 遍历完没有返回false,说明找到了合适棋盘位置了
}
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param matrix char字符型vector<vector<>>
* @param word string字符串
* @return bool布尔型
*/
bool search(vector<vector<char> >& matrix, string word,
vector<vector<bool>>& point, int i, int j, int k) {
if (k == word.size())return true;
if (i >= 0 && i <= (matrix.size() - 1) && j >= 0 &&
j <= (matrix[0].size() - 1) && point[i][j] == false &&
matrix[i][j] == word[k]) {
point[i][j] = true;
if (search(matrix, word, point, i + 1, j, k + 1) ||
search(matrix, word, point, i - 1, j, k + 1) ||
search(matrix, word, point, i, j + 1, k + 1) ||
search(matrix, word, point, i, j - 1, k + 1)) {
return true;
}
point[i][j] = false;
}
return false;
}
bool hasPath(vector<vector<char> >& matrix, string word) {
// write code here
vector<vector<bool>>point(matrix.size(), vector<bool>(matrix[0].size(), false));
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j <matrix[0].size(); j++) {
if(search(matrix, word, point, i, j, 0))
return true;
}
}
return false;
}
};
#include <algorithm>
class Solution {
public:
int renum(int n) {
int summ = 0;
while (n != 0) {
summ += n % 10;
n = (n - n % 10) / 10;
}
return summ;
}
void searchn(int threshold, int rows, int cols, int& res, int i, int j,
vector<vector<bool>>& point) {
if (i >= 0 && i <= rows - 1 && j >= 0 && j <= cols - 1 &&
renum(i) + renum(j) <= threshold&&point[i][j]==false) {
point[i][j]=true;
res++;
searchn(threshold, rows, cols, res, i + 1, j,point);
searchn(threshold, rows, cols, res, i - 1, j,point);
searchn(threshold, rows, cols, res, i, j + 1,point);
searchn(threshold, rows, cols, res, i, j - 1,point);
}
}
int movingCount(int threshold, int rows, int cols) {
int res = 0;
vector<vector<bool>>point(rows, vector<bool>(cols, false));
searchn(threshold, rows, cols, res, 0, 0, point);
return res;
}
};