491.非递减子序列
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backTrack(vector<int>& nums, int startIndex){
if(path.size() >= 2){
result.push_back(path);
}
int used[201] = {0};
for (int i = startIndex; 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;
backTrack(nums, i + 1);
path.pop_back();
}
}
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
backTrack(nums, 0);
return result;
}
};
46
class Solution {
private:
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] == true) continue;
used[i] = true;
path.push_back(nums[i]);
back(nums, used);
path.pop_back();
used[i] = false;
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<bool> used(nums.size(),false);
back(nums,used);
return result;
}
};
47 全排列2
class Solution {
private:
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] == true) continue;
used[i] = true;
path.push_back(nums[i]);
back(nums, used);
path.pop_back();
used[i] = false;
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<bool> used(nums.size(),false);
back(nums,used);
return result;
}
};