LeetCode 491 递增子序列
作者:互联网
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
int num = -101;
void dfs(int start, vector<int>& nums) {
if (path.size() > 1) {
res.push_back(path);
}
unordered_set<int> set; //只负责这一层去重,不用回溯
for (int i = start; i < nums.size(); i ++) {
if (nums[i] >= num && set.find(nums[i]) == set.end()) { //没有找到重复元素
set.insert(nums[i]);
int temp = num;
path.push_back(nums[i]);
num = nums[i];
dfs(i + 1, nums);
path.pop_back();
num = temp;
}
}
}
vector<vector<int>> findSubsequences(vector<int>& nums) {
dfs(0, nums);
return res;
}
};
标签:vector,set,nums,int,递增,491,num,path,LeetCode 来源: https://www.cnblogs.com/hjy94wo/p/16657327.html