Skip to content

Commit 46ceb72

Browse files
committed
添加 0090.子集II.md 中的Java不使用标记数组的解法
1 parent d58fe4e commit 46ceb72

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

problems/0090.子集II.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ if (i > startIndex && nums[i] == nums[i - 1] ) {
166166

167167

168168
### Java
169-
169+
使用used数组
170170
```java
171171
class Solution {
172172
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
@@ -202,6 +202,37 @@ class Solution {
202202
}
203203
```
204204

205+
不使用used数组
206+
```java
207+
class Solution {
208+
209+
List<List<Integer>> res = new ArrayList<>();
210+
LinkedList<Integer> path = new LinkedList<>();
211+
212+
public List<List<Integer>> subsetsWithDup( int[] nums ) {
213+
Arrays.sort( nums );
214+
subsetsWithDupHelper( nums, 0 );
215+
return res;
216+
}
217+
218+
219+
private void subsetsWithDupHelper( int[] nums, int start ) {
220+
res.add( new ArrayList<>( path ) );
221+
222+
for ( int i = start; i < nums.length; i++ ) {
223+
// 跳过当前树层使用过的、相同的元素
224+
if ( i > start && nums[i - 1] == nums[i] ) {
225+
continue;
226+
}
227+
path.add( nums[i] );
228+
subsetsWithDupHelper( nums, i + 1 );
229+
path.removeLast();
230+
}
231+
}
232+
233+
}
234+
```
235+
205236
### Python
206237
```python
207238
class Solution:

0 commit comments

Comments
 (0)