forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46_permutation.java
More file actions
27 lines (24 loc) · 759 Bytes
/
Copy path46_permutation.java
File metadata and controls
27 lines (24 loc) · 759 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
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
helper(nums, 0, res);
return res;
}
public void helper(int[] nums, int pos, List<List<Integer>> res){
if(pos == nums.length){
List<Integer> list = new ArrayList<Integer>();
for(int n: nums) list.add(n);
res.add(list);
}
for(int i = pos; i < nums.length; i++){
swap(nums, i, pos);
helper(nums, pos+1, res);
swap(nums, i, pos);
}
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}