forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0090Subsets2.java
More file actions
29 lines (28 loc) · 1019 Bytes
/
Copy pathP0090Subsets2.java
File metadata and controls
29 lines (28 loc) · 1019 Bytes
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class P0090Subsets2 {
List<List<Integer>>ans;
public List<List<Integer>> subsetsWithDup(int[] nums) {
ans = new ArrayList<>();
if(nums.length ==0 || nums == null) return ans;
Arrays.sort(nums);//必须sort辅助去重
backtrack(new ArrayList<>(),nums,0);
return ans;
}
public void backtrack(List<Integer>templist,int[]nums,int start){
ans.add(new ArrayList<>(templist));
for(int i = start;i<nums.length;i++){
if(i>start && nums[i]==nums[i-1])continue;// 去重
templist.add(nums[i]);
backtrack(templist,nums,i+1);
templist.remove(templist.size()-1);
}
}
}
/*
Time complexity will be O(2^n * n)
two to the power of n times n
because every element have two choices either pick or not pick.
and n extra because we are using a while loop inside the recursive function which will add n time complexity.
*/