-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermuteUnique.java
More file actions
37 lines (32 loc) · 1.19 KB
/
permuteUnique.java
File metadata and controls
37 lines (32 loc) · 1.19 KB
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class permuteUnique {
public static void main(String[] args){
int[] num = new int[]{1,1,2};
List<List<Integer>> result = allsort(num);
System.out.println(result);
}
public static List<List<Integer>> allsort(int[] nums) {
if(nums.length == 0) return new ArrayList<>();
Arrays.sort(nums);
List<List<Integer>> lists = new ArrayList<>();
int[] visited = new int[nums.length];
util( visited,nums, lists, new ArrayList<>());
return lists;
}
public static void util(int[] visited, int[] nums, List<List<Integer>> lists, List<Integer> list ) {
if (list.size() == nums.length) {
lists.add(new ArrayList<>(list));
return;
}
for(int i = 0; i < nums.length ; i++) {
if(visited[i] == 1 || (i > 0 && visited[i-1] == 0 && nums[i-1] == nums[i])) continue;
visited[i] = 1;
list.add(nums[i]);
util(visited,nums, lists, list);
list.remove(list.size() -1);
visited[i] = 0;
}
}
}