您的位置:首页 > 产品设计 > UI/UE

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

2017-11-27 21:18 399 查看
【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日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode