代码随想录图论 第四天| 827.最大人工岛 127. 单词接龙

代码随想录图论 第四天 | 827.最大人工岛 127. 单词接龙

一、827.最大人工岛

题目链接:https://leetcode.cn/problems/making-a-large-island/
思路:

class Solution {
    int[][] position = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int dfs(int[][] grid, int x, int y, int mark) {
        grid[x][y] = mark;
        int sum = 0;
        for (int[] ints : position) {
            int nx = x + ints[0];
            int ny = y + ints[1];
            if (nx < 0 || nx >= grid.length || ny < 0 || ny >= grid[0].length)continue;
            if (grid[nx][ny] == 1) {
                sum += 1 + dfs(grid, nx, ny, mark);
            }
        }
        return sum;
    }

    public int largestIsland(int[][] grid) {
        int ans = -1, size = grid.length, mark = 2;
        Map<Integer, Integer> getSize = new HashMap<>();
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                if (grid[row][col] == 1) {
                    int areaSize = 1 + dfs(grid, row, col, mark);
                    getSize.put(mark++, areaSize);
                }
            }
        }
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
            
                if (grid[row][col] != 0) continue;
                Set<Integer> hashSet = new HashSet<>();     
                int curSize = 1;
                for (int[] current: position) {
                    int curRow = row + current[0], curCol = col + current[1];
                    if (curRow < 0 || curRow >= grid.length || curCol < 0 || curCol >= grid.length) continue;
                    int curMark = grid[curRow][curCol];     
                    if (hashSet.contains(curMark) || !getSize.containsKey(curMark)) continue;
                    hashSet.add(curMark);
                    curSize += getSize.get(curMark);
                }
                ans = Math.max(ans, curSize);
            }
        }
        
        return ans == -1 ? size * size : ans;
    }
}

二、127. 单词接龙

题目链接:https://leetcode.cn/problems/word-ladder/
思路:求最短路径使用广度优先,从第一个字符串开始入队,出队后,把该字符串的每一个位置都从a到z替换,即采用广度优先的思路。

class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        HashSet<String> wordSet = new HashSet<>(wordList);
        if (wordSet.size() == 0 || !wordSet.contains(endWord)) return 0;
        HashMap<String, Integer> map = new HashMap<>();
        Deque<String> queue = new LinkedList<>();
        queue.add(beginWord);
        map.put(beginWord, 1);
        while (!queue.isEmpty()) {
            String string = queue.remove();
            Integer path = map.get(string);
            for (int i = 0; i < string.length(); i++) {
                char[] chars = string.toCharArray();
                for (char j = 'a'; j <= 'z'; j++) {
                    chars[i] = j;
                    String curStr = String.valueOf(chars);
                    if (curStr.equals(endWord)) {
                        return path+1;
                    }
                    if (wordSet.contains(curStr) && !map.containsKey(curStr)) {
                        map.put(curStr, path+1);
                        queue.add(curStr);
                    }
                }
            }
        }
        return 0;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当年拼却醉颜红

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值