-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
30 lines (30 loc) · 917 Bytes
/
Solution.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
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = i+1; j < nums.length; j++) {
if (nums[i]>nums[j]) {
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
}
int[] n=new int[nums.length];
int sum=1;
for (int i = 0; i < n.length; i++) {
n[i]=sum;
sum*=2;
}
List<List<Integer>> answer=new ArrayList<List<Integer>>();
for (int i = 0; i < sum; i++) {
List<Integer> list=new ArrayList<Integer>();
for (int j = 0; j < nums.length; j++) {
if ((i/n[j])%2==1) {
list.add(nums[j]);
}
}
answer.add(list);
}
return answer;
}
}