问题描述:
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 .
示例:
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]]
问题分析:
求解递增的子序列,可利用深度优先遍历求解,在求解过程中要注意对相同元素的处理。
过程详见代码:
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<vector<int>> res;
vector<int> re;
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++)
{
if (map.find(nums[i]) != map.end()) continue;
map[nums[i]] = 1;
re.emplace_back(nums[i]);
bl(nums, i + 1, re, res);
re.pop_back();
}
return res;
}
void bl(vector<int>& nums, int start, vector<int>& re, vector<vector<int>>& res)
{
unordered_map<int, int> map;
for (int i = start; i < nums.size(); i++)
{
if (map.find(nums[i]) != map.end()) continue;
map[nums[i]] = 1;
if (re.back() > nums[i]) continue;
re.emplace_back(nums[i]);
res.emplace_back(re);
bl(nums, i + 1, re, res);
re.pop_back();
}
}
};