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)); } }
点赞 9

相关推荐

不愿透露姓名的神秘牛友
11-08 16:12
点赞 评论 收藏
分享
牛客网
牛客企业服务