-
Notifications
You must be signed in to change notification settings - Fork 3
/
491.cpp
37 lines (36 loc) · 1.04 KB
/
491.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
string encode(vector<int>& nums) {
string s;
for (auto& num : nums) {
s += to_string(num);
s += "_";
}
return s;
}
unordered_set<string> st;
vector<vector<int>> res;
void backtracking(int index, vector<int>& nums, vector<int>& selection) {
if (index == nums.size()) {
if (selection.size() < 2) return;
string s = encode(selection);
if (!st.count(s)) {
res.push_back(selection);
st.insert(s);
}
return;
}
int n = selection.size();
if (n == 0 || selection[n - 1] <= nums[index]) {
selection.push_back(nums[index]);
backtracking(index + 1, nums, selection);
selection.pop_back();
}
backtracking(index + 1, nums, selection);
}
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<int> selection;
backtracking(0, nums, selection);
return res;
}
};