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.com/problems/increasing-subsequences/description/

C++:

class Solution {
public:
    vector<vector<int>> findSubsequences(vector<int>& nums)
    {
        set<vector<int>> res;
        vector<vector<int>> cur(1);
        for (int i = 0; i < nums.size(); ++i)
        {
            int n = cur.size();
            for (int j = 0; j < n; ++j)
            {
                if (!cur[j].empty() && cur[j].back() > nums[i])
                {
                    continue;
                }
                cur.push_back(cur[j]);
                cur.back().push_back(nums[i]);
                if (cur.back().size() >= 2)
                {
                    res.insert(cur.back());
                }
            }
        }
        return vector<vector<int>>(res.begin(), res.end());
    }
};

参考:http://www.cnblogs.com/grandyang/p/6388103.html

原文地址:https://www.cnblogs.com/xidian2014/p/8904061.html

时间: 2024-10-06 05:51:36

491 Increasing Subsequences 递增子序列的相关文章

[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

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

[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

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)

题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] O

算法实践--最长递增子序列(Longest Increasing Subsquence)

什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5}, {1} 其中最长的递增子序列是{2, 4, 5} 问题:对于长度为N的矢量D,如何找到它的最长递增子序列 一个简单的算法 for (i=N; i>0; --i) {1. 找到所有长度为i的子序列; //复杂度为(N!)/(i!)(N-i)! O(exp(N)) 2. 判断是否其中有一个为递增子

最长递增子序列(Longest increasing subsequence)

问题定义: 给定一个长度为N的数组A,找出一个最长的单调递增子序列(不要求连续). 这道题共3种解法. 1. 动态规划 动态规划的核心是状态的定义和状态转移方程.定义lis(i),表示前i个数中以A[i]结尾的最长递增子序列的长度.可以得到以下的状态转移方程: d(i) = max(1, d(j) + 1), 其中j < i,且A[j] <= A[i] 程序实现: int longestIncreasingSubsequence(vector<int> nums) { if (nu

leetcode 491. 递增子序列

给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是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]] 说明: 给定数组的长度不会超过15. 数组中的整数范围是 [-100,100]. 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况. 这种算法的复杂度O(n^2) 通过dfs的方法找到第i位数右边

HDU 5087 Revenge of LIS II(次大递增子序列)

Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1258    Accepted Submission(s): 423 Problem Description In computer science, the longest increasing subsequence problem is to f

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ