【LeetCode】491.Increasing Subsequences(Medium)解题报告

来源:互联网 发布:思迅会计软件 编辑:程序博客网 时间:2024/05/18 12:37

【LeetCode】491.Increasing Subsequences(Medium)解题报告

题目地址:https://leetcode.com/problems/increasing-subsequences/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:1. The length of the given array will not exceed 15.2. The range of integer in the given array is [-100,100].3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

  本题涉及到组合,但是有额外三个要求,每个组合多于一个元素,不重复(set解决),递增。做本题前最好先做下基本的组合的题目。

Solution:

class Solution {    public List<List<Integer>> findSubsequences(int[] nums) {        Set<List<Integer>> set = new HashSet<>();        findSub(set,nums,new ArrayList<>(),0);        return new ArrayList<>(set);    }    public void findSub(Set<List<Integer>> set, int[] nums , List<Integer> tempList,int index){        if(tempList.size()>1){            set.add(new ArrayList<>(tempList));        }        for(int i=index ; i<nums.length ; i++){            if(tempList.size()==0 || tempList.get(tempList.size()-1)<=nums[i]){                tempList.add(nums[i]);                findSub(set,nums,tempList,i+1);                tempList.remove(tempList.size() - 1);            }        }    }}

Date:2017年11月27日

原创粉丝点击