Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2.
Example:
Input: [4, 6, 7, 7] Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
Note:
- The length of the given array will not exceed 15.
- The range of integer in the given array is [-100,100].
- The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.
这道题可以通过递归的方式生成所需要的递增子序列,为了让生成的序列不重复,我们使用set库来保证序列的唯一性,最后转换为vector输出。
class Solution {
private:
set<vector<int>> res;
vector<int> nums;
void helper(vector<int> tmp, int head){
if(tmp.size()>=2) res.insert(tmp);
for(int i=head+1;i<nums.size();i++){
if(tmp.size() == 0 || nums[i]>=tmp.back()){
tmp.push_back(nums[i]);
helper(tmp, i);
tmp.pop_back();
}
}
}
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
this->nums=nums;
helper({}, -1);
vector<vector<int>> tmp(res.begin(), res.end());
return tmp;
}
};