491. Increasing Subsequences

Posted gopanama

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了491. Increasing Subsequences相关的知识,希望对你有一定的参考价值。

这道题要用Set去做
因为没有sort过 所以在后面也可能出现重复的东西

https://leetcode.com/problems/increasing-subsequences/discuss/97130/Java-20-lines-backtracking-solution-using-set-beats-100.

 

 

 1 class Solution {
 2     Set<List<Integer>> res = new HashSet<>();
 3     public List<List<Integer>> findSubsequences(int[] nums) {
 4         if(nums.length == 0) new ArrayList<>(res);
 5         visited = new boolean[nums.length];
 6         backtrack(nums, new ArrayList<>(), 0);
 7         return new ArrayList<>(res);
 8        
 9     }
10     
11     public void backtrack(int[] nums, List<Integer> list, int pos){
12         if(list.size() > 1){
13             res.add(new ArrayList<>(list));
14         }
15         
16         for(int i = pos; i < nums.length; i++){
17             if(list.size() != 0 && (nums[i] < list.get(list.size()-1))){
18                 continue;
19             }else{
20                 list.add(nums[i]);
21                 visited[i] = true;
22                 backtrack(nums, list, i+1);
23                 visited[i] = false;
24                 list.remove(list.size()-1);
25             }
26             
27             
28         }         
29         
30     }
31 }

 


以上是关于491. Increasing Subsequences的主要内容,如果未能解决你的问题,请参考以下文章

491 Increasing Subsequences

491. Increasing Subsequences

491. Increasing Subsequences

491 Increasing Subsequences

491. Increasing Subsequences

491 Increasing Subsequences 递增子序列