-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path46-permutations.java
33 lines (29 loc) · 983 Bytes
/
46-permutations.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
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result=new ArrayList<>();
Queue<List<Integer>> permutations=new LinkedList<>();
permutations.add(new ArrayList<>());
for(int i=0;i<nums.length;i++)
{
int n=permutations.size();
for(int j=0;j<n;j++)
{
List<Integer> oldPermutation=permutations.poll();
for(int k=0;k<=oldPermutation.size();k++)
{
List<Integer> newPerm=new ArrayList<Integer>(oldPermutation);
newPerm.add(k,nums[i]);
if(newPerm.size()==nums.length)
{
result.add(newPerm);
}
else
{
permutations.add(newPerm);
}
}
}
}
return result;
}
}