Skip to content

Commit

Permalink
修改
Browse files Browse the repository at this point in the history
  • Loading branch information
arkingc committed Aug 21, 2018
1 parent d38643a commit e3e5d9b
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions 数据结构与算法/算法题总结.md
Original file line number Diff line number Diff line change
Expand Up @@ -1129,22 +1129,22 @@ public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<int> subset;
vector<vector<int>> res;
subsets(nums,0,res,subset);
dfs(nums,0,res,subset);
return res;
}

private:
void subsets(const vector<int> &nums,int idx,vector<vector<int>> &res,vector<int> subset){
void dfs(const vector<int> &nums,int idx,vector<vector<int>> &res,vector<int> subset){
if(idx < 0) return;
else if(idx == nums.size()){
res.push_back(subset);
return;
}

subset.push_back(nums[idx]);
subsets(nums,idx + 1,res,subset);
dfs(nums,idx + 1,res,subset);
subset.pop_back();
subsets(nums,idx + 1,res,subset);
dfs(nums,idx + 1,res,subset);
}
};
```
Expand Down Expand Up @@ -11575,26 +11575,25 @@ public:
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
if(candidates.size() == 0) return vector<vector<int>>();

std::sort(candidates.begin(),candidates.end());
vector<vector<int>> res;
vector<int> set;
combinationSum(candidates,target,0,res,set);
dfs(candidates,target,0,res,set);

return res;
}
private:
void combinationSum(const vector<int> &candidates,int target,int idx,vector<vector<int>> &res,vector<int> &set){
if(idx == candidates.size()){
if(!target) res.push_back(set);
void dfs(const vector<int> &candidates,int target,int idx,vector<vector<int>> &res,vector<int> &set){
if(!target){
res.push_back(set);
return;
}
if(idx == candidates.size()) return;

if(target >= candidates[idx]){
combinationSum(candidates,target,idx + 1,res,set);
dfs(candidates,target,idx + 1,res,set);
set.push_back(candidates[idx]);
combinationSum(candidates,target - candidates[idx],idx,res,set);
dfs(candidates,target - candidates[idx],idx,res,set);
set.pop_back();
}
}
Expand Down Expand Up @@ -11663,25 +11662,25 @@ public:

sort(candidates.begin(),candidates.end());

combinationSum2(candidates,target,res,set,0);
dfs(candidates,target,res,set,0);

return res;
}

private:
void combinationSum2(vector<int> &candidates,int target,vector<vector<int>> &res,vector<int> &set,int idx){
void dfs(vector<int> &candidates,int target,vector<vector<int>> &res,vector<int> &set,int idx){
if(target == 0){
res.push_back(set);
return;
}
//cout << idx << endl;

if(target >= candidates[idx]){
set.push_back(candidates[idx]);
combinationSum2(candidates,target - candidates[idx],res,set,idx + 1);
dfs(candidates,target - candidates[idx],res,set,idx + 1);
set.pop_back();
while(idx < candidates.size() - 1 && candidates[idx + 1] == candidates[idx]) idx++;
if(idx != candidates.size() - 1)
combinationSum2(candidates,target,res,set,idx + 1);
dfs(candidates,target,res,set,idx + 1);
}
}
};
Expand Down Expand Up @@ -12037,15 +12036,11 @@ private:

s.insert(nums[i]);

int tp = nums[i];
nums[i] = nums[idx];
nums[idx] = tp;
swap(nums[i],nums[idx]);

permuteUnique(nums,idx + 1,res);

tp = nums[i];
nums[i] = nums[idx];
nums[idx] = tp;
swap(nums[i],nums[idx]);
}
}
};
Expand Down

0 comments on commit e3e5d9b

Please sign in to comment.