-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation.java
38 lines (33 loc) · 930 Bytes
/
permutation.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
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
Set<List<Integer>> result=new HashSet();
permutations(nums,0,result);
return new ArrayList(result);
}
void permutations(int nums[],int start,Set<List<Integer>> result)
{
if(start==nums.length){
result.add(arrayToList(nums));
}
for(int i=start;i<nums.length;i++)
{
swap(nums,i,start);
permutations(nums,start+1,result);
swap(nums,i,start);
}
}
void swap(int nums[],int i,int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
List<Integer> arrayToList(int nums[])
{
List<Integer> list=new ArrayList();
for(int i:nums){
list.add(i);
}
return list;
}
}