码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 代码随想录| 深搜、797.所有可能的路径


    深度优先搜索

    回溯算法其实就是深搜,只不过这里的深搜是侧重于在图上搜索,回溯大多是在树上搜索。

    797.所有可能的路径

    完成

    代码

    模板题

    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        // 搜索以node为根的图
        public void dfs(int[][] graph, int node) { 
            if(node == graph.length-1){
                res.add(new ArrayList<>(path));
                return;
            }
            // 遍历和node直连的所有节点
            for(int index = 0; index < graph[node].length; index++){
                path.add(graph[node][index]);
                dfs(graph, graph[node][index]);
                path.removeLast();
            }
        }
        public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
            path.add(0);
            dfs(graph, 0);
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    广度优先搜索

    二叉树的层序遍历其实就是广度优先搜索。广搜一般需要栈或队列的辅助,但不局限于栈和队列,数组也可以。

    200. 岛屿数量

    完成

    思路

    我的第一版代码在搜索过程中,只会搜索节点右边和下边的节点,这是不对的。应该搜索节点上下左右四个方向的节点,因为在搜索过程中,从哪个方向搜到该节点是未知的,比如"工"字形的岛屿,搜索时最下排的位置最先搜到中间的节点,此时要往两边扩散,而不是只管右边。

    代码

    class Solution {
        boolean[][] visited;
        int[][] move = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        public int numIslands(char[][] grid) {
            visited = new boolean[grid.length][grid[0].length];
            int res = 0;
            // 找岛屿
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if(!visited[i][j]&&grid[i][j] == '1'){
                        bfs(grid, i, j);
                        res++;
                    }
                }
            }
            return res;
        }
        // 广搜,把连通的陆地打上标记
        public void bfs(char[][] grid, int y, int x){
            Deque<int[]> queue = new LinkedList<>();
            queue.offer(new int[]{y, x});
            visited[y][x] = true;
            while (!queue.isEmpty()) {
                int[] node = queue.poll();
                for (int i = 0; i < 4; i++) {
                    int nextx = node[1] + move[i][1];
                    int nexty = node[0] + move[i][0];
                    if(nextx < 0 || nexty >= grid.length || nexty < 0 || nextx >= grid[0].length) continue;
                    if(!visited[nexty][nextx] && grid[nexty][nextx] == '1') {
                        queue.offer(new int[]{nexty, nextx}); 
                        visited[nexty][nextx] = true; //只要加入队列就标记为访问
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    记一次详细的实战渗透
    植物大战 C++ ——基础特性
    超越GPT-4V,苹果多模态大模型上新,神经形态计算加速MLLM(二)
    vue-router 重复跳转路径报错源码分析和解决方案
    Java学习 --- 面向对象之继承
    【Spring Boot】DataSource数据源的自动配置解析
    【HDLBits 刷题 8】Circuits(4)Sequential Logic---Shifts Registers & More Circuits
    《LeetCode力扣练习》代码随想录——链表(两两交换链表中的节点---Java)
    微信小程序2022年发展方向曝光
    谷粒商城 (十五) --------- 商品服务 API 品牌管理 ① 逆向工程生成前后端代码 与 前端效果优化
  • 原文地址:https://blog.csdn.net/qq_44119037/article/details/136411266
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号