import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num int整型一维数组
* @return int整型ArrayList<ArrayList<>>
*/
private void swap(ArrayList<Integer> nums, int i1, int i2) {
int temp = nums.get(i1);
nums.set(i1, nums.get(i2));
nums.set(i2, temp);
}
public void recursion(ArrayList<ArrayList<Integer>> res,
ArrayList<Integer> nums, int index) {
if (index == nums.size() - 1) {
res.add(nums);
} else {
for (int i = index; i < nums.size(); i++) {
swap(nums, index, i);
recursion(res, nums, index + 1);
swap(nums, index, i);
}
}
}
public ArrayList<ArrayList<Integer>> permute (int[] num) {
// write code here
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> nums = new ArrayList<Integer>();
Arrays.sort(num);
for (int i = 0; i < num.length; i++) {
nums.add(num[i]);
}
recursion(res, nums, 0);
return res;
}
}
点自测运行,怎么都通过不了,点保存并提交,直接就过了,搞了半天,就很无语.