leetcode-491.递增子序列

原始思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> ans = new ArrayList<>();
Set<String> set = new HashSet<>();
public List<List<Integer>> findSubsequences(int[] nums) {
dfs(-101, 0 , nums);
return result;
}

public void dfs(int pre, int begin, int[] nums){
if (begin > nums.length){
return;
}

if (ans.size() >= 2 && !set.contains(ans.toString())){
result.add(new ArrayList<Integer>(ans));
set.add(ans.toString());
}

for (int i = begin; i < nums.length; i++){
if (nums[i] < pre){
continue;
}
ans.add(nums[i]);
dfs(nums[i], i + 1, nums);
ans.remove(ans.size() - 1);
}
}
}