[LeetCode] 46. 全排列(java实现)dfs

1. 题目

2. 读题(需要重点注意的东西)

思路(dfs): dfs模板题,与完全相同,在此不再赘述。

3. 解法

---------------------------------------------------解法---------------------------------------------------:

class Solution {
          
   
    public List<List<Integer>> list = new ArrayList<>();
    public List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
          
   
        boolean[] st = new boolean[nums.length];
        dfs(nums,0,st);
        return list;
    }
    public void dfs(int[] nums,int u,boolean[] st){
          
   
        if(u == nums.length){
          
   
            list.add(new ArrayList(path));
            return;
        }
        for(int i = 0;i < nums.length;i++){
          
   
            if(st[i] == false){
          
   
                path.add(nums[i]);
                st[i] = true;
                dfs(nums,u+1,st);
                path.remove((Integer)nums[i]);
                st[i] = false;
            }
        }
    }
}

可能存在的问题:

4. 可能有帮助的前置习题

5. 所用到的数据结构与算法思想

    dfs

6. 总结

dfs模板题

经验分享 程序员 微信小程序 职场和发展