
原题链接
代码如下:
class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
public void swap(int[] nums, int i, int j ) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public void permutation(int[] nums, int left, int right) {
if(left == right) {
List<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
res.add(list);
}
for(int i = left; i <= right; i++) {
swap(nums, left, i);
permutation(nums, left + 1, right);
swap(nums, left, i);
}
}
public List<List<Integer>> permute(int[] nums) {
if(nums.length == 0) {
List<Integer> list = new ArrayList<Integer>();
res.add(list);
return res;
}
permutation(nums, 0, nums.length - 1);
return res;
}
}