新增java用set2k

This commit is contained in:
Lozakaka
2023-05-08 18:10:28 -04:00
committed by GitHub
parent a039519df2
commit 898c42a990

View File

@ -203,6 +203,30 @@ public:
### Java ### Java
```Java
//using set, aligned with the unimproved method
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backTracking(nums, 0);
return result;
}
private void backTracking(int[] nums, int startIndex){
if(path.size() >= 2)
result.add(new ArrayList<>(path));
HashSet<Integer> hs = new HashSet<>();
for(int i = startIndex; i < nums.length; i++){
if(!path.isEmpty() && path.get(path.size() -1 ) > nums[i] || hs.contains(nums[i]))
continue;
hs.add(nums[i]);
path.add(nums[i]);
backTracking(nums, i + 1);
path.remove(path.size() - 1);
}
}
}
```
```java ```java
class Solution { class Solution {