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

        }
    }

  

时间: 2024-10-13 10:39:39

491. Increasing Subsequences的相关文章

491 Increasing Subsequences 递增子序列

给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2.示例:输入: [4, 6, 7, 7]输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]说明:    1.给定数组的长度不会超过15.    2.数组中的整数范围是 [-100,100].    3.给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况.详见:https://leetcode.c

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

LightOJ 1085 - All Possible Increasing Subsequences (离散化+树状数组+dp)

1085 - All Possible Increasing Subsequences   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold i1 <

[Swift]LeetCode491. 递增子序列 | 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

LightOJ 1085 - All Possible Increasing Subsequences 树状数组+离散

http://www.lightoj.com/volume_showproblem.php?problem=1085 题意:求一个序列的递增子序列个数. 思路:找规律可以发现,某个数作为末尾数的种类数为所有比它小的数的情况+1.使用树状数组查找即可. C++11 的auto在Lightoj上不能用/.\ /** @Date : 2016-12-01-21.58 * @Author : Lweleth ([email protected]) * @Link : https://github.com

【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组

题意 给定$n$个数,求最长上升子序列的方案数 根据数据范围要求是$O(n\log n)$ 朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i=v_i+v_j,(f_i=f_j+1)$,利用lis的$O(n\log n)$树状数组做法同时维护长度和方案数 从通酱博客里还看到更详尽的解释:stackoverflow 时间复杂度$O(n\log n)$ 代码 #include <bits/stdc++.h> using namespace

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

打卡2

1.   155. Min Stack https://leetcode.com/problems/min-stack/#/description 借助一个栈存储最小值, 当入栈值小于等于当前最小值时, 才加入中间站..当最小值被被pop出时, 中间栈才pop最小值 中间站初始的时候要加入一个第一个值 public class MinStack { private Stack<Integer> stack = new Stack<Integer>(); private Stack&

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.