forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_491.java
35 lines (31 loc) · 1.2 KB
/
_491.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class _491 {
public static class Solution1 {
public List<List<Integer>> findSubsequences(int[] nums) {
if (nums == null || nums.length == 1) {
return new ArrayList<>();
}
Set<List<Integer>> answer = new HashSet<>();
List<Integer> list = new ArrayList<>();
return new ArrayList<>(backtracking(nums, 0, list, answer));
}
private Set<List<Integer>> backtracking(int[] nums, int start, List<Integer> currList,
Set<List<Integer>> answer) {
if (currList.size() >= 2) {
answer.add(new ArrayList<>(currList));
}
for (int i = start; i < nums.length; i++) {
if (currList.size() == 0 || currList.get(currList.size() - 1) <= nums[i]) {
currList.add(nums[i]);
backtracking(nums, i + 1, currList, answer);
currList.remove(currList.size() - 1);
}
}
return answer;
}
}
}