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
491 Increasing Subsequences 递增子序列的更多相关文章
- [LeetCode] 491. Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
- [LeetCode] Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
- 【LeetCode】491. Increasing Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 491. Increasing Subsequences增长型序列
[抄题]: Given an integer array, your task is to find all the different possible increasing subsequence ...
- LeetCode 491. Increasing Subsequences
原题链接在这里:https://leetcode.com/problems/increasing-subsequences/ 题目: Given an integer array, your task ...
- 491. Increasing Subsequences
这种increasing xxx 题真是老客户了.. 本题麻烦点在于不能重复, 但是和之前的那些 x sum的题目区别在于不能排序的 所以.... 我还是没搞定. 看了一个Java的思路是直接用set ...
- 【leetcode】491. Increasing Subsequences
题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...
- Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences)
Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences) 深度优先搜索的解题详细介绍,点击 给定一个整型数组, 你的任务是找到所有该数组 ...
- [Swift]LeetCode491. 递增子序列 | Increasing Subsequences
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
随机推荐
- hadoop shuffle
1 hadoop shuffle的地位 hadoop shuffle是map reduce算法的核心,是它连接了多个map和多个reduce,它将map的输出交给reduce作为输入. 2 hado ...
- Cats transport(codeforces311B)(斜率优化)
\(Cats Transport\) 感觉这道题题面不好讲,就自翻了一个新的,希望有助于大家理解其思路: 大致题意: \(wch\) 的家里有 \(N\) 座山(山呈直线分布,第 \(i-1\) 座山 ...
- VC FTP服务器程序分析(一)
想在QT上移植一个FTP服务器程序,先学习windows下的FTP服务器例子,然后随便动手写点东西. 在pudn上搜索 "FTP服务器端和客户端实现 VC“这几个关键字,就可以搜到下面要分析 ...
- leetcode 677. Map Sum Pairs
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
- docker pure-ftp 搭建ftp服务器
参考:https://hub.docker.com/r/stilliard/pure-ftpd/ docker-compose.yml: ftp: image: stilliard/pure-ftpd ...
- 用HTTP操作和文件操作把网页下载到sd卡
这里先把代码贴到这里做一个存档,写到SD卡的是一个txt文件,改成HTML格式之后会出现百度主页,但是中文是乱码,这一点先暂时不去研究了. 代码: package com.larry.gotcha; ...
- [Selenium] HTML5 中的 Geolocation
在 HTML5 ,新特性 Geolocation 用于定位用户位置信息. 由于用户位置信息是敏感信息,所以需要得到用户允许后,才能让程序通过 API 获取当前用户信息.WebDriver 程序每次重 ...
- Struts2的各种标签库
1 在JSP中使用taglib编译指令导入标签库 <%@ taglib prefix="s" uri="/struts-tags" %> ----- ...
- [USACO 2016Dec] Team Building
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4742 [算法] 动态规划 用Fi,j,k表示约翰的前i头牛和保罗的前j头牛匹配 , ...
- 高性能框架gevent和gunicorn在web上的应用及性能测试
WSGI Server有哪些: 比如 Flask,webpy,Django.CherryPy 都带着 WSGI server .当然性能都不好,自带的web server 更多的是测试用途, 发布时则 ...