-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy path78.subsets.1.java
59 lines (58 loc) · 1.08 KB
/
78.subsets.1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* @lc app=leetcode id=78 lang=java
*
* [78] Subsets
*
* https://leetcode.com/problems/subsets/description/
*
* algorithms
* Medium (52.37%)
* Likes: 2152
* Dislikes: 53
* Total Accepted: 391.7K
* Total Submissions: 725K
* Testcase Example: '[1,2,3]'
*
* Given a set of distinct integers, nums, return all possible subsets (the
* power set).
*
* Note: The solution set must not contain duplicate subsets.
*
* Example:
*
*
* Input: nums = [1,2,3]
* Output:
* [
* [3],
* [1],
* [2],
* [1,2,3],
* [1,3],
* [2,3],
* [1,2],
* []
* ]
*
*/
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> answer = new ArrayList<>();
int bitNumber = nums.length;
int power = 1 << bitNumber;
for (int i = 0; i < power; i++) {
List<Integer> list = new ArrayList<>();
int bit = i;
int count = 0;
while (bit != 0) {
if ((bit & 1) == 1) {
list.add(nums[count]);
}
count++;
bit = bit >> 1;
}
answer.add(new ArrayList<>(list));
}
return answer;
}
}