zoukankan      html  css  js  c++  java
  • 491. Increasing Subsequences

    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]]
    

    Constraints:

    • 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.
    public class Solution {
        public List<List<Integer>> findSubsequences(int[] nums) {
            List<List<Integer>> res = new LinkedList<>();
            helper(new LinkedList<Integer>(), 0, nums, res);
            return res; 
        }
        private void helper(LinkedList<Integer> list, int index, int[] nums, List<List<Integer>> res){
            if(list.size()>1) res.add(new LinkedList<Integer>(list));
            Set<Integer> used = new HashSet<>();
            for(int i = index; i<nums.length; i++){
                if(used.contains(nums[i])) continue;
                if(list.size()==0 || nums[i]>=list.peekLast()){
                    used.add(nums[i]);
                    list.add(nums[i]); 
                    helper(list, i+1, nums, res);
                    list.remove(list.size()-1);
                }
            }
        }
    }

    服了,就是简单backtracking,又没做出来

    https://leetcode.com/problems/increasing-subsequences/discuss/97147/Java-solution-beats-100

  • 相关阅读:
    面试题
    学习Javascript闭包(Closure)
    git命令
    css3 3d翻转效果
    meta 如何写
    Java String equals和==的比较
    MySQL Explain详解
    MySQL explain,type分析(转)
    python的内存分析和处理
    python lambda的使用
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13409115.html
Copyright © 2011-2022 走看看