题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> permute (int[] num) { Arrays.sort(num); ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); recursion(res, num, 0); return res; } public void swap(int[] num, int i, int j) { int temp = num[i]; num[i] = num[j]; num[j] = temp; } public void recursion(ArrayList<ArrayList<Integer>> res, int[] num, int x) { if (x == num.length - 1) { ArrayList<Integer> temp = new ArrayList<>(); for (int i = 0; i < num.length; i++) temp.add(num[i]); res.add(temp); } for (int i = x; i < num.length; i++) { swap(num, i, x); recursion(res, num, x + 1); swap(num, i, x); } } }