Skip to content

Commit 2ba50b8

Browse files
committed
0090. Subsets II
1 parent 3df593d commit 2ba50b8

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

markdown/0090. Subsets II.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
### [90\. Subsets II](https://leetcode.com/problems/subsets-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a collection of integers that might contain duplicates, **_nums_**, return all possible subsets (the power set).
7+
8+
**Note:** The solution set must not contain duplicate subsets.
9+
10+
**Example:**
11+
12+
```
13+
Input: [1,2,2]
14+
Output:
15+
[
16+
[2],
17+
[1],
18+
[1,2,2],
19+
[2,2],
20+
[1,2],
21+
[]
22+
]
23+
```
24+
25+
26+
#### Solution
27+
28+
Language: **Java**
29+
30+
```java
31+
class Solution {
32+
   public List<List<Integer>> subsetsWithDup(int[] nums) {
33+
       List<List<Integer>> result = new ArrayList<>();
34+
       List<Integer> elem = new ArrayList<>();
35+
       result.add(elem);
36+
       Arrays.sort(nums);
37+
       for (int i = 0; i < nums.length; i++) {
38+
           int dupCount = 0;
39+
           while (((i + 1) < nums.length) && nums[i + 1] == nums[i]) {
40+
               dupCount++;
41+
               i++;
42+
          }
43+
           int size = result.size();
44+
           for (int j = 0; j < size; j++) {
45+
               elem = new ArrayList<>(result.get(j));
46+
               for (int k = 0; k <= dupCount; k++) {
47+
                   elem.add(nums[i]);
48+
                   result.add(new ArrayList<>(elem)); // 注意这里是要放在 for 循环里面的
49+
              }
50+
          }
51+
      }
52+
       return result;
53+
  }
54+
}
55+
```
56+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-03-1F2FZ6.jpg)

src/main/java/leetcode/_90_/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._90_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[] nums = {1, 2, 2};
10+
System.out.println(solution.subsetsWithDup(nums));
11+
}
12+
}
13+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode._90_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
class Solution {
8+
public List<List<Integer>> subsetsWithDup(int[] nums) {
9+
List<List<Integer>> result = new ArrayList<>();
10+
List<Integer> elem = new ArrayList<>();
11+
result.add(elem);
12+
Arrays.sort(nums);
13+
for (int i = 0; i < nums.length; i++) {
14+
int dupCount = 0;
15+
while (((i + 1) < nums.length) && nums[i + 1] == nums[i]) {
16+
dupCount++;
17+
i++;
18+
}
19+
int size = result.size();
20+
for (int j = 0; j < size; j++) {
21+
elem = new ArrayList<>(result.get(j));
22+
for (int k = 0; k <= dupCount; k++) {
23+
elem.add(nums[i]);
24+
result.add(new ArrayList<>(elem)); // 注意这里是要放在 for 循环里面的
25+
}
26+
}
27+
}
28+
return result;
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
### [90\. Subsets II](https://leetcode.com/problems/subsets-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a collection of integers that might contain duplicates, **_nums_**, return all possible subsets (the power set).
7+
8+
**Note:** The solution set must not contain duplicate subsets.
9+
10+
**Example:**
11+
12+
```
13+
Input: [1,2,2]
14+
Output:
15+
[
16+
[2],
17+
[1],
18+
[1,2,2],
19+
[2,2],
20+
[1,2],
21+
[]
22+
]
23+
```
24+
25+
26+
#### Solution
27+
28+
Language: **Java**
29+
30+
```java
31+
class Solution {
32+
   public List<List<Integer>> subsetsWithDup(int[] nums) {
33+
       List<List<Integer>> result = new ArrayList<>();
34+
       List<Integer> elem = new ArrayList<>();
35+
       result.add(elem);
36+
       Arrays.sort(nums);
37+
       for (int i = 0; i < nums.length; i++) {
38+
           int dupCount = 0;
39+
           while (((i + 1) < nums.length) && nums[i + 1] == nums[i]) {
40+
               dupCount++;
41+
               i++;
42+
          }
43+
           int size = result.size();
44+
           for (int j = 0; j < size; j++) {
45+
               elem = new ArrayList<>(result.get(j));
46+
               for (int k = 0; k <= dupCount; k++) {
47+
                   elem.add(nums[i]);
48+
                   result.add(new ArrayList<>(elem)); // 注意这里是要放在 for 循环里面的
49+
              }
50+
          }
51+
      }
52+
       return result;
53+
  }
54+
}
55+
```
56+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-03-1F2FZ6.jpg)

0 commit comments

Comments
 (0)