Merge pull request #2082 from Lozakaka/patch-11

新增java用set的解法
This commit is contained in:
程序员Carl
2023-06-07 07:28:04 +08:00
committed by GitHub

View File

@ -205,6 +205,30 @@ public:
### 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
class Solution {