题目
代码如下:
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
bool occured(vector<int>& nums, int key, int startindex){
for(int i = startindex; i < key; i++){
if(nums[key] == nums[i]){
return true;
}
}
return false;
}
void backtrace(vector<int>& nums, int startindex, int length){
if(length >= 2 && path.size() == length){
//去重
res.push_back(path);
}
if(startindex >= nums.size()){
//控制子序列为非递减
return ;
}
for(int i = startindex; i < nums.size(); i++){
if(i != startindex && occured(nums, i,startindex)){
continue;
}
if(nums[i] < path.back()){
continue;
}
path.push_back(nums[i]);
backtrace(nums, i+1, length+1);
path.pop_back();
}
}
vector<vector<int>> findSubsequences(vector<int>& nums) {
res.clear();
path.clear();
for(int i = 0; i < nums.size(); i++){
//先在path中遍历第一个元素
//这一步写在backtrace里的话会很不好处理
if(i != 0 && occured(nums, i,0)){
continue;
}
path.push_back(nums[i]);
backtrace(nums,i+1, 1);
path.pop_back();
}
return res;
}
};
如果把对path第一个元素的处理也写在backtrace里的话:
没写出来,不写了
标签:nums,int,back,力扣,startindex,vector,序列,path,递减
From: https://www.cnblogs.com/satsuki26681534/p/18062792