组合
int*a;
int asize;
int** aa;
int aasize;
void backtracking(int n, int k,int start)
{if(asize==k)
{int*tmp=(int*)malloc(sizeof(int)*k);
int i;
for(i=0;i<k;i++)
{tmp[i]=a[i];}
aa[aasize++]=tmp;
return ;
}
int j;
for(j=start;j<=n;j++)
{a[asize++]=j;
backtracking(n,k,j+1);
asize--;
}
}
int** combine(int n, int k, int* returnSize, int** returnColumnSizes) {
a=(int*)malloc(sizeof(int)*k);
asize=0;
aa=(int**)malloc(sizeof(int*)*1000000);
aasize=0;
backtracking(n, k,1);
*returnSize=aasize;
*returnColumnSizes=(int*)malloc(sizeof(int)*(*returnSize));
int i;
for( i=0;i<*returnSize;i++)
{
(*returnColumnSizes)[i]=k;
}
return aa;
}
组合总和
int* a;
int asize;
int**aa;
int aasize;
void backtracking(int n,int k,int start,int sum)
{if(k==asize)
{if(n==sum)
{int*tmp=(int*)malloc(sizeof(int)*k);
for(int i=0;i<k;i++)
{tmp[i]=a[i];
}
aa[aasize++]=tmp;
}
return ;
}
for(int i=start;i<=9;i++)
{a[asize++]=i;
sum+=i;
backtracking(n,k,i+1,sum);
sum-=i;
asize--;
}
}
int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes) {
a=(int*)malloc(sizeof(int)*k);
asize=0;
aa=(int**)malloc(sizeof(int*)*20);
aasize=0;
backtracking(n,k,1,0);
* returnSize=aasize;
*returnColumnSizes=(int*)malloc(sizeof(int)*(* returnSize));
for(int i=0;i<* returnSize;i++)
{(*returnColumnSizes)[i]=k;
}
return aa;
}
电话号码的字母组合
class Solution {
private:
const string letterMap[10] = {
"",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz",
};
public:
vector<string> result;
string s;
void backtracking(string& digits,int index)
{if(index==digits.size())
{result.push_back(s);
return ;
}
int letter=digits[index]-'0';
string letters=letterMap[letter];
for(int i=0;i<letters.size();i++)
{s.push_back(letters[i]);
backtracking(digits,index+1);
s.pop_back();
}
}
vector<string> letterCombinations(string digits) {
s.clear();
result.clear();
if(digits.size()==0)
return result;
backtracking(digits,0);
return result;
}
};
组合总和3
class Solution {
vector<vector<int>> result;
vector<int>path;
void backtracking(vector<int>& candidates,int target,int sum,int start)
{if(sum>target)
return;
if(sum==target)
{result.push_back(path);
return ;
}
for(int i=start;i<candidates.size();i++)
{sum+=candidates[i];
path.push_back(candidates[i]);
backtracking(candidates,target,sum,i);
sum-=candidates[i];
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
result.clear();
path.clear();
backtracking(candidates,target,0,0);
return result;
}
};
组合总和2
class Solution {
private:
vector<vector<int>> result;
vector<int>path;
void backtracking(vector<int>& candidates, int target,int sum,int start,vector<bool>& used)
{if(sum>target)
return ;
if(sum==target)
{result.push_back(path);
return ;
}
for(int i=start;i<candidates.size();i++)
{if(i>0&&used[i-1]==false&&candidates[i]==candidates[i-1])
continue;
path.push_back(candidates[i]);
sum+=candidates[i];
used[i]=true;
backtracking(candidates,target,sum,i+1,used);
path.pop_back();
sum-=candidates[i];
used[i]=false;
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
result.clear();
path.clear();
vector<bool>used(candidates.size(),false);
sort(candidates.begin(),candidates.end());
backtracking(candidates,target,0,0,used);
return result;
}
};
分割字符串
class Solution {
public:
vector<vector<string>>result;
vector<string>path;
bool isPalindrome(string s,int index,int i)
{int begin=index;
int end=i;
while(begin<end)
{if(s[begin]!=s[end])
return false;
begin++;
end--;
}
return true;
}
void backtracking(string& s,int index)
{if(index>=s.size())
{result.push_back(path);
return ;
}
for(int i=index;i<s.size();i++)
{if(isPalindrome(s,index,i))
{string str=s.substr(index,i-index+1);
path.push_back(str);
}
else
{continue;}
backtracking(s,i+1);
path.pop_back();
}
}
vector<vector<string>> partition(string s) {
result.clear();
path.clear();
backtracking(s,0);
return result;
}
};
复原ip地址
class Solution {
public:
vector<string>result;
bool isvalid(string s,int begin,int end)
{
if(begin>end)
return false;
if(s[begin]=='0'&&begin!=end)
{return false;
}
int num=0;
for(int i=begin;i<=end;i++)
{
if(s[i]<'0'||s[i]>'9')
return false;
num=num*10+s[i]-'0';
if(num>255)
return false;
}
return true;
}
void backtracking(string&s ,int pointnum,int index)
{
if(pointnum==3)
{if(isvalid(s,index,s.size()-1))
result.push_back(s);
return ;
}
for(int i=index;i<=s.size();i++)
{if(isvalid(s,index,i))
{s.insert(s.begin()+i+1,'.');
pointnum++;
backtracking(s ,pointnum,i+2);
s.erase(s.begin()+i+1);
pointnum--;
}
else
break;
}
}
vector<string> restoreIpAddresses(string s) {
result.clear();
backtracking(s ,0,0);
return result;
}
};
子集
class Solution {
public:
vector<vector<int>>result;
vector<int>path;
void backtracking(vector<int>& nums,int index)
{ result.push_back(path);
if(index>=nums.size())
return ;
for(int i=index;i<nums.size();i++)
{path.push_back(nums[i]);
backtracking(nums,i+1);
path.pop_back();
}
}
vector<vector<int>> subsets(vector<int>& nums) {
path.clear();
result.clear();
backtracking(nums,0);
return result;
}
};
子集2
class Solution {
public:
vector<vector<int>> result;
vector<int>path;
void backtracking(vector<int>& nums,int index,vector<bool>&used)
{result.push_back(path);
if(index>=nums.size())
return ;
for(int i=index;i<nums.size();i++)
{if(i>0&&nums[i-1]==nums[i]&&used[i-1]==0)
continue;
else
{path.push_back(nums[i]);
used[i]=true;
backtracking(nums,i+1,used);
used[i]=false;
path.pop_back();
}
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
path.clear();
result.clear();
vector<bool>used(nums.size(),false);
sort(nums.begin(),nums.end());
backtracking(nums,0,used);
return result;
}
};
非递减序列
class Solution {
public:
vector<vector<int>>result;
vector<int>path;
void backtracking(vector<int>& nums,int index)
{if(path.size()>1)
result.push_back(path);
int used[201]={0};
for(int i=index;i<nums.size();i++)
{if((!path.empty()&&nums[i]<path.back())||used[nums[i]+100]==1)
{ continue;}
path.push_back(nums[i]);
used[nums[i]+100]=1;
backtracking(nums,i+1);
path.pop_back();
}
}
vector<vector<int>> findSubsequences(vector<int>& nums) {
result.clear();
path.clear();
backtracking(nums,0);
return result;
}
};
全排列
class Solution {
public:
vector<vector<int>> result;
vector <int>path;
void backtracking(vector<int>& nums,vector<bool>&used)
{if(path.size()>=nums.size())
result.push_back(path);
for(int i=0;i<nums.size();i++)
{if(used[i]==true)continue;
path.push_back(nums[i]);
used[i]=true;
backtracking(nums,used);
used[i]=false;
path.pop_back();
}
}
vector<vector<int>> permute(vector<int>& nums) {
path.clear();
result.clear();
vector<bool>used(nums.size(),false);
backtracking(nums,used);
return result;
}
};
全排列2
class Solution {
vector<vector<int>> result;
vector<int> path;
void backtracking(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-1]==nums[i]&&used[i-1]==true)
{continue;
}
if(used[i]==false)
{used[i]=true;
path.push_back(nums[i]);
backtracking(nums,used);
path.pop_back();
used[i]=false;
}
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
path.clear();
result.clear();
vector<bool>used(nums.size(),false);
sort(nums.begin(),nums.end());
backtracking(nums,used);
return result;
}
};
N皇后
class Solution {
public:
vector<vector<string>> result;
void backtracking(int n,int row,vector<string>&chessboard)
{if(row==n)
{result.push_back(chessboard);
return ;}
for(int i=0;i<n;i++)
{if(isvalid(chessboard,i,row,n))
{chessboard[row][i]='Q';
backtracking(n,row+1,chessboard);
chessboard[row][i]='.';
}
}
}
bool isvalid(vector<string>&chessboard,int col,int row,int n)
{for(int i=0;i<row;i++)
{if(chessboard[i][col]=='Q')
return false;
}
for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--)
{
if(chessboard[i][j]=='Q')
return false;
}
for(int i=row-1,j=col+1;i>=0&&j<n;i--,j++)
{
if(chessboard[i][j]=='Q')
return false;
}
return true;
}
vector<vector<string>> solveNQueens(int n) {
result.clear();
std::vector<std::string> chessboard(n, std::string(n, '.'));
backtracking(n,0,chessboard);
return result;
}
};
解数独
class Solution {
public:
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]=='.')
{for(char k='1';k<='9';k++)
{if(invalid(board,i,j,k))
{board[i][j]=k;
if(backtracking(board))return true;
board[i][j]='.';
}
}
return false;
}
}
}
return true;
}
bool invalid(vector<vector<char>>&board,int row,int col,char k)
{for(int i=0;i<9;i++)
{if(board[row][i]==k)
return false;}
for(int i=0;i<9;i++)
{if(board[i][col]==k)
return false;}
int startx=(row/3)*3;
int starty=(col/3)*3;
for(int i=startx;i<startx+3;i++)
{for(int j=starty;j<starty+3;j++)
{if(board[i][j]==k)
return false;
}
}
return true;
}
void solveSudoku(vector<vector<char>>& board) {
backtracking(board);
}
};