491. Increasing Subsequences
来源:互联网 发布:mac充电 编辑:程序博客网 时间:2024/05/12 13:45
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.
原本的想法是从原数组减去一个数,但这样就不符合增序的要求。所以反过来,从单个数往上加,加的时候判断一下是否符合条件。
public class Solution { public List<List<Integer>> findSubsequences(int[] nums) { Set<List<Integer>> set=new HashSet<>(); LinkedList<Integer> hold=new LinkedList<>(); helper(hold,set,nums,0); List res=new ArrayList(set); return res; } public void helper(LinkedList<Integer> hold, Set<List<Integer>> set,int[] nums ,int index){ if(hold.size()>=2) set.add(new LinkedList<>(hold)); for(int i=index;i<nums.length;i++){ if(hold.size()==0||hold.get(hold.size()-1)<=nums[i]){ hold.add(nums[i]); helper(hold,set,nums,i+1); hold.remove(hold.size()-1); } } }}
0 0
- 【LeetCode】 491. Increasing Subsequences
- 491. Increasing Subsequences**
- 491. Increasing Subsequences
- LeetCode 491. Increasing Subsequences
- 491. Increasing Subsequences
- 491. Increasing Subsequences
- LeetCode 491. Increasing Subsequences
- [LeetCode]491. Increasing Subsequences
- 491. Increasing Subsequences
- [leetcode]491. Increasing Subsequences
- 【Leetcode】491. Increasing Subsequences
- [leetcode] 491. Increasing Subsequences
- 491. Increasing Subsequences
- Leetcode -- 491. Increasing Subsequences
- leetcode-491. Increasing Subsequences
- (LeetCode) 491. Increasing Subsequences
- 491. Increasing Subsequences
- LeetCode 491. Increasing Subsequences
- springMVC源码分析--视图View(一)
- 9.来电显示归属地--自定义Toast
- Linux驱动技术(八) _并发控制技术
- mysql如何开启关闭外部连接
- ***CodeForces - 743B Chloe and the sequence
- 491. Increasing Subsequences
- 剑指:用两个栈实现队列的先进先出
- task_struct--Linux结构体字段介绍
- java Iterator迭代器
- 12.synchronized
- Elasticsearch5.20 简单安装使用Kibana
- 关于子元素选择器
- Linux下用rpm方式安装MySQL
- cocoaPods的使用问题