-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek 4 47. Permutations II
More file actions
31 lines (29 loc) · 995 Bytes
/
week 4 47. Permutations II
File metadata and controls
31 lines (29 loc) · 995 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
30
31
public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0){
return res;
}
Arrays.sort(nums);
helper(res, new ArrayList<Integer>(), new boolean [nums.length], nums);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> path, boolean[] visited, int[] nums){
if(path.size() == nums.length){
res.add(new ArrayList<Integer>(path));
return;
}
for (int i = 0; i < nums.length; i++){
if (visited[i] || (i != 0 && nums[i] == nums[i-1] && visited[i-1]))
{
continue;
}
path.add(nums[i]);
visited[i] = true;
helper(res, path, visited, nums);
path.remove(path.size() - 1);
visited[i] = false;
}
return;
}
}