1.https://leetcode.com/problems/subsets/description/
这里要注意下次递归的起点是i而非pos
class Solution {
void subsetsAux(vector<vector<int>>& ans,vector<int>& vt,vector<int>& nums,int pos){
ans.push_back(vt);
for(int i=pos;i<nums.size();++i){
vt.push_back(nums[i]);
subsetsAux(ans,vt,nums,i+1);
vt.pop_back();
}
}
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> vt;
subsetsAux(ans,vt,nums,0);
return ans;
}
};
2.https://leetcode.com/problems/subsets-ii/description/
问题主要在于元素存在重复,在深搜时遇到重复元素就continue,需要注意的是:深搜前需要对元素进行排序才能保证不会有重复set出现。
class Solution {
private:
void subsetsWithDupAux(vector<vector<int>>& ans,vector<int>& vt,vector<int>& nums,int pos){
ans.push_back(vt);
for(int i=pos;i<nums.size();++i){
if(i>pos && nums[i]==nums[i-1])
continue;
vt.push_back(nums[i]);
subsetsWithDupAux(ans,vt,nums,i+1);
vt.pop_back();
}
}
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> vt;
sort(nums.begin(),nums.end());
subsetsWithDupAux(ans,vt,nums,0);
return ans;
}
};
3.https://leetcode.com/problems/permutations/description/
注意下次递归时起点是pos+1而非i+1
class Solution {
void permuteAux(vector<vector<int>>& ans,vector<int>& nums,int pos){
if(pos==nums.size()){
ans.push_back(nums);
return;
}
for(int i=pos;i<nums.size();++i){
swap(nums[pos],nums[i]);
permuteAux(ans,nums,pos+1);
swap(nums[pos],nums[i]);
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
permuteAux(ans,nums,0);
return ans;
}
};
4.https://leetcode.com/problems/permutations-ii/description/
需要注意的是在遍历完所有可能节点后,通过rotate来恢复子序列。比如1234,遍历完成后是4123,rotate后为1234
class Solution {
private:
void permuteUniqueAux(vector<vector<int>>& ans,vector<int>& nums,int pos){
if(pos==nums.size()){
ans.push_back(nums);
return;
}
for(int i=pos;i<nums.size();++i){
if(i>pos && nums[i]==nums[pos])
continue;
swap(nums[i],nums[pos]);
permuteUniqueAux(ans,nums,pos+1);
}
rotate(nums.begin()+pos,nums.begin()+pos+1,nums.end());
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> ans;
permuteUniqueAux(ans,nums,0);
return ans;
}
};
5.https://leetcode.com/problems/combination-sum/description/
注意push结果的条件是target等于0
class Solution {
private:
void combinationSumAux(vector<vector<int>>& ans,vector<int>& nums,vector<int>& candidates, int pos,int target){
if(target==0){
ans.push_back(nums);
return;
}
for(int i=pos;i<candidates.size();++i){
if(candidates[i]<=target){
nums.push_back(candidates[i]);
combinationSumAux(ans,nums,candidates,i,target-candidates[i]);
nums.pop_back();
}
}
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> nums;
combinationSumAux(ans,nums,candidates,0,target);
return ans;
}
};
6.https://leetcode.com/problems/combination-sum-ii/description/
同问题2,为了避免重复,需要实现排序原数组并且忽略重复的元素。
class Solution {
void combinationSum2Aux(vector<vector<int>>& ans,vector<int>& nums,vector<int>& candidates, int target,int pos){
if(target==0){
ans.push_back(nums);
return;
}
for(int i=pos;i<candidates.size();++i){
if(i>pos && candidates[i]==candidates[i-1])
continue;
if(candidates[i]<=target){
nums.push_back(candidates[i]);
combinationSum2Aux(ans,nums,candidates,target-candidates[i],i+1);
nums.pop_back();
}
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector<vector<int>> ans;
vector<int> nums;
combinationSum2Aux(ans,nums,candidates,target,0);
return ans;
}
};
7.https://leetcode.com/problems/palindrome-partitioning/description/
通过DP+DFS的方式判断。
class Solution {
private:
void partitionAux(vector<vector<bool>>& f,vector<vector<string>>& ans,vector<string>& vs,string& s,int pos){
if(pos==s.size()){
ans.push_back(vs);
return;
}
for(int i=1;pos+i<=s.size();++i){
string sub=s.substr(pos,i);
if(f[pos][pos+i-1]){
vs.push_back(sub);
partitionAux(f,ans,vs,s,pos+i);
vs.pop_back();
}
}
}
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> ans;
vector<string> vs;
int n=s.size();
vector<vector<bool>> f(n,vector<bool>(n,false));
for(int i=0;i<n;++i)
f[i][i]=true;
for(int len=2;len<=n;++len){
for(int i=0;i<n-len+1;++i){
int j=i+len-1;
if(s[i]!=s[j])
f[i][j]=false;
else{
if(i+1==j)
f[i][j]=true;
else
f[i][j]=f[i+1][j-1];
}
}
}
partitionAux(f,ans,vs,s,0);
return ans;
}
};