diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 5b92517a..a1e86464 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -166,7 +166,7 @@ if (i > startIndex && nums[i] == nums[i - 1] ) { ### Java - +使用used数组 ```java class Solution { List> result = new ArrayList<>();// 存放符合条件结果的集合 @@ -202,6 +202,37 @@ class Solution { } ``` +不使用used数组 +```java +class Solution { + + List> res = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + + public List> 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: