首页 > 学院 > 开发设计 > 正文

[leetcode]491. Increasing Subsequences

2019-11-06 06:50:33
字体:
来源:转载
供稿:网友

题目链接:https://leetcode.com/PRoblems/increasing-subsequences/?tab=Description

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.

解法一:

class Solution{public:    vector<vector<int>> findSubsequences(vector<int>& nums)    {        set<vector<int>> res;        vector<int> tmp;        dfs(nums,res,tmp,0);        return vector<vector<int>>(res.begin(),res.end());    }    void dfs(vector<int>& nums,set<vector<int>> &res,vector<int> tmp,int k)    {        if(tmp.size()>=2)        {                res.insert(tmp);        }        for(int i=k;i<nums.size();i++)        {            if(tmp.size()==0||tmp.back()<=nums[i]){            tmp.push_back(nums[i]);            dfs(nums,res,tmp,i+1);            tmp.pop_back();}        }    }};


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表