代码随想录——图论一刷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;
    }
}
相关推荐
二哈喇子!1 小时前
若依【(前后端分离版)SpringBoot+Vue3】
java·spring boot·后端
paopaokaka_luck1 小时前
婚纱摄影管理系统(发送邮箱、腾讯地图API、物流API、webSocket实时聊天、协同过滤算法、Echarts图形化分析)
vue.js·spring boot·后端·websocket·算法·echarts
愚戏师2 小时前
机器学习(重学版)基础篇(算法与模型一)
人工智能·算法·机器学习
Monkey-旭4 小时前
Android Handler 完全指南
android·java·handler
秃狼4 小时前
Execel文档批量替换标签实现方案
java
神经兮兮的小饼4 小时前
字符串是数据结构还是数据类型?
数据结构·字符串
Brookty4 小时前
Java线程安全与中断机制详解
java·开发语言·后端·学习·java-ee
OEC小胖胖4 小时前
渲染篇(二):解密Diff算法:如何用“最少的操作”更新UI
前端·算法·ui·状态模式·web
找不到、了4 小时前
Java排序算法之<归并排序>
算法·排序算法
香蕉可乐荷包蛋4 小时前
排序算法 (Sorting Algorithms)-Python示例
python·算法·排序算法