方法:回溯 class Solution { List<List<Integer>> ans = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> permute(int[] nums) { boolean[] used = new boolean[nums.length]; backtracking(nums, used); return ans; } public void backtracking(int[] nums, boolean[] used) { if (path.size() == nums.length) { ans.add(new ArrayList<>(path)); return; } for (int i = 0; i < nums.length; i++) { if (!used[i]) { path.add(nums[i]); used[i] = true; //向下一层递归 backtracking(nums, used); //撤销上一次的选择 path.remove(path.size() - 1); used[i] = false; } } } }