mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
新增java用set2k
This commit is contained in:
@ -203,6 +203,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 {
|
||||
|
Reference in New Issue
Block a user