import java.util.ArrayList; import java.util.List; public class Solution { public List<List<Integer>> getArrange(int n){ boolean[] vis = new boolean[n+1]; List<List<Integer>> rsl = new ArrayList<>(); dfs(new ArrayList<>(), rsl, vis, 1, n, 1); return rsl; } public void dfs(List<Integer> rs, List<List<Integer>> rsl, boolean[] vis, int i, int n, int depth){ if(depth == n + 1){ if(rs.size()>=1) rsl.add(new ArrayList<>(rs)); return; } for(int j=i;j<=n;j++){ if(!vis[j]){ vis[j] = true; rs.add(j); dfs(rs, rsl, vis, j+1, n, depth + 1); vis[j] = false; rs.remove(rs.size()-1); dfs(rs, rsl, vis, j+1, n , depth+1); } } } public static void main(String[] args) { Solution s = new Solution(); System.out.println(s.getArrange(3)); } }