class Solution {
public:
vector<int> path;
vector<vector<int>> result;
void backtracking(vector<int>& nums, int startIndex)
{
if(path.size()>1)
{
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;
backtracking(nums,i+1);
path.pop_back();
}
}
vector<vector<int>> findSubsequences(vector<int>& nums) {
backtracking(nums,0);
return result;
}
};
class Solution {
public:
vector<int> path;
vector<vector<int>> result;
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(used[nums[i]+10]==true){
continue;
}
path.push_back(nums[i]);
used[nums[i]+10]=true;
backtracking(nums,used);
used[nums[i]+10]=false;
path.pop_back();
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<bool> used(21,false);
backtracking(nums,used);
return result;
}
};
class Solution {
public:
vector<int> path;
vector<vector<int>> result;
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]==nums[i-1] && used[i-1]==false)
{
continue;
}
if(used[i]==false)
{
path.push_back(nums[i]);
used[i]=true;
backtracking(nums,used);
used[i]=false;
path.pop_back();
}
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<bool> used(nums.size(),false);
sort(nums.begin(), nums.end()); // 排序
backtracking(nums,used);
return result;
}
};
2024.2.25 18:40-19:40