import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> permute (int[] num) { // write code here int length = num.length; ArrayList<ArrayList<Integer>> lists = new ArrayList<> (); if(length == 0) return lists; Stack<Integer> path = new Stack<>(); boolean[] isUsed = new boolean[length]; dfs(num, length, 0, path, isUsed, lists); return lists; } private static void dfs(int[] num, int len, int depth, Stack<Integer> path, boolean[] isUsed, ArrayList<ArrayList<Integer>> lists){ if(depth == len){ lists.add(new ArrayList<Integer>(path)); return; }else{ for(int i = 0; i < len; i++){ if(isUsed[i]) continue; path.add(num[i]); isUsed[i] = true; dfs(num, len, depth+1, path, isUsed, lists); isUsed[i] = false; path.pop(); } } } }