-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets II
More file actions
23 lines (23 loc) · 1 KB
/
Subsets II
File metadata and controls
23 lines (23 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Solution {
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
// Note: The Solution object is instantiated only once and is reused by each test case.
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(num.length == 0) return result;
Arrays.sort(num); //non-descending order
result.add(new ArrayList<Integer>());
for(int i=0; i<num.length; i++)
{
int current = num[i];
ArrayList<ArrayList<Integer>> temp = new ArrayList<ArrayList<Integer>>();
for(ArrayList<Integer> item : result)
{
ArrayList<Integer> cache = new ArrayList<Integer>(item); //need to create a new instance, or will replcace the old one
cache.add(current);
if(!result.contains(cache)) // avoid adding duplicates
temp.add(cache);
}
result.addAll(temp);
}//end for loop
return result;
}
}