原题链接:Leecode 491. 递增子序列 自己写的代码:每一个层次利用set去重
class Solution {
public:
vector<vector<int>> res;
vector<int> v;
void dfs(vector<int>& nums,int start)
{
if(start==nums.size()) return;
set<int> s;
for(int i=start;i<nums.size();i++)
{
if(s.count(nums[i])) continue;
if(v.size()>0 && nums[i]<v.back()) continue;
if(v.size()>0 && nums[i]>=v.back())
{
v.push_back(nums[i]);
res.push_back(v);
}
if(v.size()==0) v.push_back(nums[i]);
s.insert(nums[i]);
dfs(nums,i+1);
if(v.size()>0 ) v.pop_back();
}
}
vector<vector<int>> findSubsequences(vector<int>& nums) {
dfs(nums,0);
return res;
}
};
|