-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSolution491.java
39 lines (34 loc) · 1.07 KB
/
Solution491.java
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
30
31
32
33
34
35
36
37
38
39
package algorithm.leetcode;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* @author: mayuan
* @desc: 递增子序列
* @date: 2019/03/15
*/
public class Solution491 {
public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> ans = new LinkedList<>();
dfs(ans, new LinkedList<>(), nums, 0);
return ans;
}
public void dfs(List<List<Integer>> answer, LinkedList<Integer> oneAnswer, int[] nums, int start) {
if (oneAnswer.size() > 1) {
answer.add(new LinkedList<>(oneAnswer));
}
Set<Integer> set = new HashSet<>();
for (int i = start; i < nums.length; ++i) {
if (set.contains(nums[i])) {
continue;
}
if (0 == oneAnswer.size() || nums[i] >= oneAnswer.peekLast()) {
set.add(nums[i]);
oneAnswer.add(nums[i]);
dfs(answer, oneAnswer, nums, i + 1);
oneAnswer.remove(oneAnswer.size() - 1);
}
}
}
}