思路:
首先数组内每个元素不重复,所以对于当前位置如果选择某一个位置,那么在之后该元素就不能参与排序。可以使用递归实现,代码如下:
class Solution {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
process(nums, 0, ans);
return ans;
}
public static void process(int[] nums, int index, List<List<Integer>> ans) {
if (index == nums.length) {
ArrayList<Integer> cur = new ArrayList<>();
for (int num : nums) {
cur.add(num);
}
ans.add(cur);
} else {
for (int j = index; j < nums.length; j++) {
swap(nums, index, j);
process(nums, index + 1, ans);
swap(nums, index, j);
}
}
}
public static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}