代码随想录——图论一刷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;
    }
}
相关推荐
GISer_Jing2 分钟前
前端算法实战:大小堆原理与应用详解(React中优先队列实现|求前K个最大数/高频元素)
前端·算法·react.js
一路向北North27 分钟前
IDEA加载项目时依赖无法更新
java·ide·intellij-idea
小森77671 小时前
(三)机器学习---线性回归及其Python实现
人工智能·python·算法·机器学习·回归·线性回归
振鹏Dong1 小时前
超大规模数据场景(思路)——面试高频算法题目
算法·面试
uhakadotcom1 小时前
Python 与 ClickHouse Connect 集成:基础知识和实践
算法·面试·github
uhakadotcom1 小时前
Python 量化计算入门:基础库和实用案例
后端·算法·面试
小萌新上大分1 小时前
SpringCloudGateWay
java·开发语言·后端·springcloud·springgateway·cloudalibaba·gateway网关
uhakadotcom2 小时前
使用 Python 与 BigQuery 进行交互:基础知识与实践
算法·面试
uhakadotcom2 小时前
使用 Hadoop MapReduce 和 Bigtable 进行单词统计
算法·面试·github
XYY3692 小时前
前缀和 一维差分和二维差分 差分&差分矩阵
数据结构·c++·算法·前缀和·差分