mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 0090.子集II.md Java版本
This commit is contained in:
@ -172,7 +172,40 @@ if (i > startIndex && nums[i] == nums[i - 1] ) {
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
|
||||
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
|
||||
boolean[] used;
|
||||
public List<List<Integer>> subsetsWithDup(int[] nums) {
|
||||
if (nums.length == 0){
|
||||
result.add(path);
|
||||
return result;
|
||||
}
|
||||
Arrays.sort(nums);
|
||||
used = new boolean[nums.length];
|
||||
subsetsWithDupHelper(nums, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void subsetsWithDupHelper(int[] nums, int startIndex){
|
||||
result.add(new ArrayList<>(path));
|
||||
if (startIndex >= nums.length){
|
||||
return;
|
||||
}
|
||||
for (int i = startIndex; i < nums.length; i++){
|
||||
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
|
||||
continue;
|
||||
}
|
||||
path.add(nums[i]);
|
||||
used[i] = true;
|
||||
subsetsWithDupHelper(nums, i + 1);
|
||||
path.removeLast();
|
||||
used[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user