mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加 0090.子集II.md 中的Java不使用标记数组的解法
This commit is contained in:
@ -166,7 +166,7 @@ if (i > startIndex && nums[i] == nums[i - 1] ) {
|
|||||||
|
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
使用used数组
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
|
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
|
||||||
@ -202,6 +202,37 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
不使用used数组
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
List<List<Integer>> res = new ArrayList<>();
|
||||||
|
LinkedList<Integer> path = new LinkedList<>();
|
||||||
|
|
||||||
|
public List<List<Integer>> subsetsWithDup( int[] nums ) {
|
||||||
|
Arrays.sort( nums );
|
||||||
|
subsetsWithDupHelper( nums, 0 );
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void subsetsWithDupHelper( int[] nums, int start ) {
|
||||||
|
res.add( new ArrayList<>( path ) );
|
||||||
|
|
||||||
|
for ( int i = start; i < nums.length; i++ ) {
|
||||||
|
// 跳过当前树层使用过的、相同的元素
|
||||||
|
if ( i > start && nums[i - 1] == nums[i] ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
path.add( nums[i] );
|
||||||
|
subsetsWithDupHelper( nums, i + 1 );
|
||||||
|
path.removeLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
Reference in New Issue
Block a user