添加 0090.子集II.md 中的Java不使用标记数组的解法

This commit is contained in:
erdengk
2022-01-02 11:21:56 +08:00
parent d58fe4ea60
commit 46ceb72cd0

View File

@ -166,7 +166,7 @@ if (i > startIndex && nums[i] == nums[i - 1] ) {
### Java
使用used数组
```java
class Solution {
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
class Solution: