491. 递增子序列(回溯+剪枝)

class Solution {
    List<Integer> temp = new ArrayList<Integer>();
    List<List<Integer>> ans = new ArrayList<List<Integer>>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        dfs(0, -101, nums);
        return ans;
    }
    public void dfs(int cur, int last, int[] nums) {
        if (cur == nums.length) {
            if (temp.size() >= 2) ans.add(new ArrayList<Integer>(temp));
            return;
        }
        if (nums[cur] >= last) {
            temp.add(nums[cur]);
            dfs(cur + 1, nums[cur], nums);
            temp.remove(temp.size() - 1);
        }
        if (nums[cur] != last) dfs(cur + 1, last, nums); // 和上一个值不相等才递归到下一层
    }
}

 

posted @ 2020-08-25 19:47  Sexyomaru  阅读(87)  评论(0编辑  收藏  举报