码迷,mamicode.com
首页 > 其他好文 > 详细

491. Increasing Subsequences

时间:2017-08-08 09:29:36      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:color   should   lis   array   highlight   diff   rem   contain   input   

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.

带重复节点的subset

 public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        Arrays.sort(nums);
        helper(nums, 0, ans, list);
        return ans;
        
    }
    private void helper(int[] nums, int pos, List<List<Integer>> ans, List<Integer> list) {
        if (list.size() >= 2) {
           
            ans.add(new ArrayList(list));
        }
        for (int i = pos; i < nums.length; i++) {
            if (i != pos && nums[i - 1] == nums[i]){
                continue;
            }
           
                list.add(nums[i]);
                helper(nums, i + 1, ans, list);
                list.remove(list.size() - 1);
            
        }
    }

  

491. Increasing Subsequences

标签:color   should   lis   array   highlight   diff   rem   contain   input   

原文地址:http://www.cnblogs.com/apanda009/p/7304914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!