题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
import java.util.*; public class Solution { public ArrayList<ArrayList<Integer>> permute(int[] num) { ArrayList<ArrayList<Integer>> ans = new ArrayList<>(); boolean[] isVisited = new boolean[num.length]; int length = num.length; DFS(num, isVisited, ans, new ArrayList<>(), length); return ans; } public void DFS(int[] num, boolean[] isVisited, ArrayList<ArrayList<Integer>> ans, ArrayList<Integer> temp, int length) { if (length == 0) { ans.add(new ArrayList<>(temp)); return; } else { for (int i = 0; i < num.length; i++) { if (!isVisited[i]) { isVisited[i] = true; temp.add(num[i]); DFS(num, isVisited, ans, temp, length - 1); temp.remove(temp.size() - 1); isVisited[i] = false; } } } } }