491 Increasing Subsequences
Posted tobeabetterpig
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:? 1. The length of the given array will not exceed 15. 2. The range of integer in the given array is [-100,100]. 3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence. class Solution { public List<List<Integer>> findSubsequences(int[] nums) { Set<List<Integer>> set = new HashSet<>(); List<Integer> tempList = new ArrayList<>(); dfs(set, tempList, nums, 0); return new ArrayList<>(set); // new ArrayList<>(); } private void dfs(Set<List<Integer>> set, List<Integer> tempList, int[] nums, int index){ if(tempList.size() > 1){ set.add(new ArrayList<>(tempList)); } // add numbers for(int i = index; i < nums.length; i++){ if(tempList.size() == 0 || tempList.get(tempList.size() - 1) <= nums[i]){ tempList.add(nums[i]); dfs(set, tempList, nums, i + 1 ); // be careful , not index + 1 tempList.remove(tempList.size() - 1); } } } } https://www.youtube.com/watch?v=dl68syidyLM&t=242s 注意reference 写法。。这个还不熟 画 dfs 图 https://leetcode.com/problems/increasing-subsequences/discuss/97130/Java-20-lines-backtracking-solution-using-set-beats-100.
以上是关于491 Increasing Subsequences的主要内容,如果未能解决你的问题,请参考以下文章