Skip to content

Commit cfbbad1

Browse files
Add files via upload
1 parent 64b2c95 commit cfbbad1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 基本思路依然是求子集的思路
2+
// 引入哈希表是为了解决这种情况 [1,2,3,4,5,6,7,8,9,10,1,1,1,1,1]
3+
4+
// Runtime: 196 ms, faster than 81.72% of C++ online submissions for Increasing Subsequences.
5+
// Memory Usage: 21.1 MB, less than 61.15% of C++ online submissions for Increasing Subsequences.
6+
7+
class Solution
8+
{
9+
public:
10+
vector<vector<int>> findSubsequences(vector<int>& nums)
11+
{
12+
vector<vector<int>> res;
13+
vector<int> candidate;
14+
15+
helper(res, candidate, nums, 0);
16+
17+
return res;
18+
}
19+
private:
20+
void helper(vector<vector<int>>& res, vector<int>& candidate, vector<int>& nums, int start)
21+
{
22+
if (candidate.size() >= 2)
23+
res.push_back(candidate);
24+
25+
unordered_set<int> hashSet;
26+
for (int i = start; i < nums.size(); ++i)
27+
{
28+
if (!candidate.empty() && candidate.back() > nums[i])
29+
continue;
30+
if (hashSet.find(nums[i]) == hashSet.end())
31+
{
32+
candidate.push_back(nums[i]);
33+
helper(res, candidate, nums, i + 1);
34+
candidate.pop_back();
35+
hashSet.insert(nums[i]);
36+
}
37+
}
38+
}
39+
};

0 commit comments

Comments
 (0)