-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathArrayPermutation.java
More file actions
32 lines (32 loc) · 847 Bytes
/
ArrayPermutation.java
File metadata and controls
32 lines (32 loc) · 847 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
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
permute(nums,0,list);
return list;
}
public static void swap(int nums[],int x,int y)
{
int p = nums[x];
nums[x] = nums[y];
nums[y] = p;
}
public static void permute(int nums[],int index,List<List<Integer>> list)
{
if(index==nums.length-1)
{
List<Integer> l = new ArrayList<>();
for(int i=0;i<nums.length;i++)
{
l.add(nums[i]);
}
list.add(l);
return;
}
for(int i=index;i<nums.length;i++)
{
swap(nums,i,index);
permute(nums,index+1,list);
swap(nums,index,i);
}
}
}