力扣--回溯篇(2)

最近在疯狂摆烂啊啊啊啊

11.子集 90. 子集 II - 力扣(LeetCode)

java 复制代码
class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    //关键在于去重
    public void backTracing(int i,int[] nums)
    {
        if(i==nums.length)
        {ans.add(new ArrayList<>(path));
        return;}
        //选x
        int x=nums[i];
        path.add(x);
        backTracing(i+1,nums);
        path.removeLast();

        //不选,那么后面所有等于x的数都不选,不然会导致重复
        i++;
        while(i<nums.length&&nums[i]==x)
        {
            i++;
        }
        backTracing(i,nums);
    }
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        backTracing(0,nums);
        return ans;
    }
}

12.递增子序列 491. 非递减子序列 - 力扣(LeetCode)

13.全排列 46. 全排列 - 力扣(LeetCode)

java 复制代码
class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public void swap(int a, int b)
    {
        int x=path.get(a);
        path.set(a,path.get(b));
        path.set(b,x);
    }

    public void backTracing(int i, int[] nums)
    {
        if(i==nums.length)
        {
            ans.add(new ArrayList<>(path));
            return;
        }
        for(int j=i;j<nums.length;j++)
        {
            swap(i,j);
            backTracing(i+1,nums);
            swap(i,j);
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        for(int num:nums)
        {
            path.add(num);
        }
        backTracing(0,nums);
        return ans;
    }
}

14.全排列

java 复制代码
class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> path = Arrays.asList(new Integer[nums.length]); // 所有排列的长度都是 n
        boolean[] onPath = new boolean[n]; // onPath[j] 表示 nums[j] 是否已经填入排列
        dfs(0, nums, path, onPath, ans);
        return ans;
    }

    // i 表示当前要填排列的第几个数
    private void dfs(int i, int[] nums, List<Integer> path, boolean[] onPath, List<List<Integer>> ans) {
        if (i == nums.length) { // 填完了
            ans.add(new ArrayList<>(path));
            return;
        }
        // 枚举 nums[j] 填入 path[i]
        for (int j = 0; j < nums.length; j++) {
            // 如果 nums[j] 已填入排列,continue
            // 如果 nums[j] 和前一个数 nums[j-1] 相等,且 nums[j-1] 没填入排列,continue
            if (onPath[j] || j > 0 && nums[j] == nums[j - 1] && !onPath[j - 1]) {
                continue;
            }
            path.set(i, nums[j]); // 填入排列
            onPath[j] = true; // nums[j] 已填入排列(注意标记的是下标,不是值)
            dfs(i + 1, nums, path, onPath, ans); // 填排列的下一个数
            onPath[j] = false; // 恢复现场
            // 注意 path 无需恢复现场,因为排列长度固定,直接覆盖 path[i] 就行
        }
    }
}

15.N皇后

16.单词搜索 79. 单词搜索 - 力扣(LeetCode)

java 复制代码
class Solution {
    //标记是否匹配到
    private boolean found = false;
    //四个方向
    private int[][] dirs={{-1,0},{1,0},{0,1},{0,-1}};

    private void dfs(char[][] board, String word, int i, int x, int y, boolean[][] visited){
        //找到单词
        if(found)
        {
            return;
        }
        //匹配到末尾 说明找到
        if(i==word.length())
        {
            found=true;
            return;
        }
        //越界或者不等于 直接返回
        int rows=board.length,cols=board[0].length;
        if(x<0||x>=rows||y<0||y>=cols||board[x][y]!=word.charAt(i)||visited[x][y]==true)
        {
            return;
        }
        visited[x][y]=true;
        for(int[] dir : dirs){
            int newX=x+dir[0];
            int newY=y+dir[1];
            dfs(board,word,i+1,newX,newY,visited);
        }
        //回溯
        visited[x][y]=false;
    }

    public boolean exist(char[][] board, String word) {
        if(board==null||board.length==0||board[0].length==0||word==null||word.isEmpty()){
            return false;
        }
        int rows=board.length;
        int cols=board[0].length;
        boolean[][] visited=new boolean[rows][cols];
        for(int i=0;i<rows;i++){
            for(int j=0;j<cols;j++){
                if(!visited[i][j])
                dfs(board,word,0,i,j,visited);
                if(found){
                    return true;
                }
            }
        }
        return false;
    }
}
相关推荐
夏鹏今天学习了吗3 小时前
【LeetCode热题100(82/100)】单词拆分
算法·leetcode·职场和发展
mit6.8243 小时前
mysql exe
算法
2501_901147834 小时前
动态规划在整除子集问题中的应用与高性能实现分析
算法·职场和发展·动态规划
中草药z4 小时前
【嵌入模型】概念、应用与两大 AI 开源社区(Hugging Face / 魔塔)
人工智能·算法·机器学习·数据集·向量·嵌入模型
CCPC不拿奖不改名4 小时前
SQL基础(SQL小白教程):MySQL语句+环境一键搭建+面试习题
数据库·sql·计算机网络·mysql·oracle·面试·职场和发展
踩坑记录4 小时前
leetcode hot100 189.轮转数组 medium
leetcode
知乎的哥廷根数学学派4 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
ADI_OP5 小时前
ADAU1452的开发教程10:逻辑算法模块
算法·adi dsp中文资料·adi dsp·adi音频dsp·adi dsp开发教程·sigmadsp的开发详解
xingzhemengyou15 小时前
C语言 查找一个字符在字符串中第i次出现的位置
c语言·算法
Dream it possible!6 小时前
LeetCode 面试经典 150_二分查找_在排序数组中查找元素的第一个和最后一个位置(115_34_C++_中等)
c++·leetcode·面试