-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSubsets.java
83 lines (74 loc) · 2.55 KB
/
Subsets.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*https://leetcode.com/problems/subsets/*/
/*https://practice.geeksforgeeks.org/problems/subsets-1613027340/1*/
/*Prateekshya*/
class Solution {
List<List<Integer>> result;
public List<List<Integer>> subsets(int[] nums) {
result = new ArrayList<List<Integer>>();
recur(nums,0, new ArrayList<Integer>());
return result;
}
public void recur(int[] nums, int index, ArrayList<Integer> currList)
{
if (index == nums.length) //if all elements are checked
{
//create a list with the currently chosen elements
List<Integer> newList = new ArrayList<Integer>();
for (Integer element : currList)
newList.add(element);
//add it to the result and return
result.add(newList);
return;
}
/*exclude the current element and recur for the next*/
recur(nums,index+1,currList);
/*include the current element and recur for the next*/
currList.add(nums[index]);
recur(nums,index+1,currList);
currList.remove(currList.size()-1);
}
}
class Solution
{
static ArrayList<ArrayList<Integer>> result;
static int n;
public static void recur(ArrayList<Integer> currList, int val, ArrayList<Integer> A)
{
if (val == n)
{
ArrayList<Integer> newList = new ArrayList<Integer>();
for (Integer element : currList)
newList.add(element);
result.add(newList);
return;
}
recur(currList, val+1, A);
currList.add(A.get(val));
recur(currList, val+1, A);
currList.remove(currList.size()-1);
}
public static ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> A)
{
//code here
result = new ArrayList<ArrayList<Integer>>();
n = A.size();
recur(new ArrayList<Integer>(), 0, A);
Collections.sort(result, new Comparator<ArrayList<Integer>>()
{
public int compare(ArrayList<Integer> list1, ArrayList<Integer> list2)
{
int i = 0, j = 0;
if (list1.size() == 0) return -1;
if (list2.size() == 0) return 1;
while ((Integer)list1.get(i) == (Integer)list2.get(j))
{
++i; ++j;
if (i >= list1.size()) return -1;
if (j >= list2.size()) return 1;
}
return (Integer)list1.get(i)-(Integer)list2.get(j);
}
});
return result;
}
}