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

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

文章目录

  • 前言
  • 一、力扣127. 单词接龙
  • 二、力扣841.钥匙和房间
  • 三、力扣463. 岛屿的周长


前言


一、力扣127. 单词接龙

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.钥匙和房间

有向图深度搜索

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);
        }
    }
}

有向图广度搜索

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. 岛屿的周长

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

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 ++;
                }
            }
        }
    }
}

最丑陋的一集

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;
    }
}

你可能感兴趣的:(图论,深度优先,算法,java,数据结构,leetcode)