-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutationunique.java
More file actions
35 lines (33 loc) · 933 Bytes
/
Copy pathpermutationunique.java
File metadata and controls
35 lines (33 loc) · 933 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
32
33
34
35
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> ans=new ArrayList<>();
findans(ans,nums,0);
return ans;
}
public void findans(List<List<Integer>> ans,int nums[],int start)
{
if(start==nums.length)
{
ArrayList <Integer> toadd=new ArrayList<>();
for(int i=0;i<nums.length;i++)
{
toadd.add(nums[i]);
}
ans.add(new ArrayList(toadd));
return;
}
HashSet<Integer> hs=new HashSet<>();
for(int i=start;i<nums.length;i++)
{
if(hs.add(nums[i]))
{
int temp=nums[start];
nums[start]=nums[i];
nums[i]=temp;
findans(ans,nums,start+1);
nums[i]=nums[start];
nums[start]=temp;
}
}
}
}