代码随想录——图论一刷day04

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言


一、力扣127. 单词接龙

java 复制代码
class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        Set<String> wordSet = new HashSet<>(wordList);
        if(wordList.size() == 0 || !wordSet.contains(endWord)){
            return 0;
        }
        Deque<String> deq = new LinkedList<>();
        deq.offerLast(beginWord);
        Map<String, Integer> map = new HashMap<>();
        map.put(beginWord,1);
        while(!deq.isEmpty()){
            String cur = deq.pollFirst();
            int path = map.get(cur);
            for(int i = 0; i < cur.length(); i ++){
                char[] ch = cur.toCharArray();
                for(char k = 'a'; k <= 'z'; k ++){
                    ch[i] = k;
                    String newCur = String.valueOf(ch);
                    if(newCur.equals(endWord)){
                        return path + 1;
                    }
                    if(wordSet.contains(newCur) && !map.containsKey(newCur)){
                        map.put(newCur, path + 1);
                        deq.offerLast(newCur);
                    }
                }
            }
        }
        return 0;
    }
}

二、力扣841.钥匙和房间

有向图深度搜索

java 复制代码
class Solution {
    boolean[] flag;
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        flag = new boolean[rooms.size()];
        dfs(rooms, 0);
        for(boolean f : flag){
            if(f == false){
                return false;
            }
        }
        return true;
    }
    public void dfs(List<List<Integer>> rooms, int key){
        if(flag[key]){
            return;
        }
        flag[key] = true;
        for(Integer in : rooms.get(key)){
            dfs(rooms, in);
        }
    }
}

有向图广度搜索

java 复制代码
class Solution {
    boolean[] flag;
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        flag = new boolean[rooms.size()];
        bfs(rooms, 0);
        flag[0] = true;
        for(boolean f : flag){
            if(f == false){
                return false;
            }
        }
        return true;
    }
    public void bfs(List<List<Integer>> rooms, int key){
        Deque<List<Integer>> deq = new LinkedList<>();
        deq.offerLast(rooms.get(key));
        while(!deq.isEmpty()){
            List<Integer> cur = deq.pollFirst();
            for(Integer in :cur){
                if(flag[in] == false){
                    deq.offerLast(rooms.get(in));
                    flag[in] = true;
                }
            }
        }
    }
}

三、力扣463. 岛屿的周长

递归遍历无向图,一边统计节点个数,一边统计边数

java 复制代码
class Solution {
    boolean[][] flag;
    int path, count;
    int[][] move = {{0,1},{0,-1},{-1,0},{1,0}};
    public int islandPerimeter(int[][] grid) {
        flag = new boolean[grid.length][grid[0].length];
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[0].length; j ++){
                if(grid[i][j] == 1 && flag[i][j] == false){
                    bfs(grid, i, j);
                }
            }
        }
        return path * 4 - count;
    }
    public void bfs(int[][] grid, int x, int y){
        Deque<int[]> deq = new LinkedList<>();
        deq.offerLast(new int[]{x,y});
        path = 1;
        flag[x][y] = true;
        while(!deq.isEmpty()){
            int[] cur = deq.pollFirst();
            for(int i = 0; i < 4; i ++){
                int nextX = cur[0] + move[i][0];
                int nextY = cur[1] + move[i][1];
                if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length || grid[nextX][nextY] == 0){
                    continue;
                }
                count ++;
                if(flag[nextX][nextY] == false){
                    flag[nextX][nextY] = true;
                    deq.offerLast(new int[]{nextX, nextY});
                    path ++;
                }
            }
        }
    }
}

最丑陋的一集

java 复制代码
class Solution {
    public int islandPerimeter(int[][] grid) {
        int[][] move = {{1,0},{-1,0},{0,-1},{0,1}};
        int count = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[0].length; j ++){
                if(grid[i][j] == 1){
                    for(int t = 0; t < 4; t ++){
                        int nextX = i + move[t][0];
                        int nextY = j + move[t][1];
                        if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length){
                            count ++;
                        }else{
                            if(grid[nextX][nextY] == 0){
                                count ++;
                            }
                        }
                    }
                }
            }
        }
        return count;
    }
}
相关推荐
noipp几秒前
推荐题目:洛谷 P5843 [SCOI2012] Blinker 的噩梦
c语言·数据结构·c++·算法·游戏·洛谷·luogu
Mark_ZP9 分钟前
【锁2】锁的分类与概念
java·
冻柠檬飞冰走茶17 分钟前
PTA基础编程题目集 7-20 打印九九口诀表(C语言实现)
c语言·开发语言·数据结构·算法
鱼子星_18 分钟前
《算 · 法》题目解析篇(1):最近公共祖先问题,线段树,最长回文子序列
c++·算法·动态规划·递归
雾喔20 分钟前
算法练习5
算法·代理模式
caishenzhibiao23 分钟前
顺势捕猎者副图 同花顺期货通指标
java·c语言·c#
问商十三载34 分钟前
RAG 检索召回越多越好?2026 信噪比模型深度解析
人工智能·算法·机器学习
额恩6636 分钟前
ResearchPilot 第三阶段总结报告
python·算法
Hi李耶1 小时前
【LeetCode】9-回文数
算法·leetcode·职场和发展
红叶舞1 小时前
基于线段树的数据结构
数据结构·python·算法