代码随想录——图论一刷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;
    }
}
相关推荐
apcipot_rain24 分钟前
C++转Java日记(10)
java·开发语言
简 洁 冬冬1 小时前
泛型方法写法全面辨析:从「能编译」到「能跑」到「能用」
java·泛型
YLCHUP1 小时前
【思维&图论建模】题解:债权(debt)_图论_建模_思路转化_思维题目_C++算法竞赛
c++·笔记·算法·数学建模·贪心算法·深度优先·图论
LGL6030A1 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习
思麟呀2 小时前
背包DP(01背包+完全背包)
c++·算法
笑容掩饰我的失落3 小时前
区块链从小白到精通(七):Solidity 语法精讲——数据类型、函数、修饰器与事件
算法·区块链·php·智能合约·哈希算法
Lansonli3 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark
budingxiaomoli4 小时前
Spring AOP
java·spring·java-ee
特立独行的猫A4 小时前
Ionic + 仓颉语言的 cj-tauri 开发框架实战:开发精美移动端 APP
java·后端·python
Pniubi4 小时前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表