博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 491. Increasing Subsequences
阅读量:7144 次
发布时间:2019-06-29

本文共 1273 字,大约阅读时间需要 4 分钟。

Problem

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.

Solution

class Solution {    public List
> findSubsequences(int[] nums) { List
> res = new ArrayList<>(); if (nums == null || nums.length == 0) return res; dfs(nums, 0, new ArrayList
(), res); return res; } private void dfs(int[] nums, int start, List
temp, List
> res) { if (temp.size() > 1) { res.add(new ArrayList<>(temp)); } Set
used = new HashSet<>(); for (int i = start; i < nums.length; i++) { if (used.contains(nums[i])) continue; if (temp.size() == 0 || nums[i] >= temp.get(temp.size()-1)) { temp.add(nums[i]); used.add(nums[i]); dfs(nums, i+1, temp, res); //next dfs doesn't have used, yeah temp.remove(temp.size()-1); } } }}

转载地址:http://gswgl.baihongyu.com/

你可能感兴趣的文章
字典操作
查看>>
[洛谷P2839][国家集训队]middle
查看>>
《求一个数组的连续的最大子数组之和》
查看>>
设置行间距,自适应文字大小
查看>>
资金流学习-广州发展
查看>>
python基础3(元祖、字典、深浅copy、集合、文件处理)
查看>>
正确编写Designated Initializer的几个原则
查看>>
iOS播放动态GIF图片
查看>>
获取版本号
查看>>
使用jdk自带的visualVM监控远程监控was
查看>>
集合视图UICollectionView 介绍及其示例程序
查看>>
JsLint 的安装和使用
查看>>
合并傻子//区间dp
查看>>
让IE和Chrome都以隐身模式启动
查看>>
MyPython-->进阶篇-->类
查看>>
unity remote 连接设置
查看>>
2018 NOIP备战计划
查看>>
教你如何迅速秒杀掉:99%的海量数据处理面试题
查看>>
Silverlight如何调用淘宝API
查看>>
ESP8266- AP模式的使用
查看>>