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

news/2024/7/15 5:50:56 标签: 图论, 算法, 数据结构, leetcode, java, 深度优先

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

一、827.最大人工岛

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

java">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替换,即采用广度优先的思路。

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

http://www.niftyadmin.cn/n/5135131.html

相关文章

GaussDB SQL基础语法示例-常见的条件表达式

目录 一、前言 二、条件表达式的概念及GaussDB中的常见条件表达式 三、GaussDB中常用的条件表达式&#xff08;语法 示例&#xff09; 1、CASE表达式 2、DECODE表达式 3、COALESCE表达式 4、NULLIF表达式 5、GREATEST/ LEAST表达式 6、NVL表达式 四、小结 一、前言 …

用软件模拟IPC的RTSP流,对接烟火识别算法服务,做实时的烟火检测、人员入侵检测、抽烟检测等算法

最近在研发烟火识别的算法&#xff0c;想要检验算法集成到视频分析服务之后的效果&#xff0c;发现线上的摄像机很难发现火情&#xff0c;有的很长时间都不会有检测的结果&#xff0c;于是我就需要用已经被检验过的视频文件&#xff0c;模拟一路IPC的RTSP流&#xff0c;来测试烟…

41. 缺失的第一个正数 --力扣 --JAVA

题目 给你一个未排序的整数数组 nums &#xff0c;请你找出其中没有出现的最小的正整数。 请你实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案。 解题思路 对数组进行排序&#xff0c;便于查看是否连续&#xff1b;因为是最小正整数&#xff0c;所以判断值应从1…

【Python机器学习】零基础掌握RidgeClassifierCV线性分类器

如何在医疗领域更准确地预测乳腺癌? 假设在一家医院里,医生拿到了一批乳腺癌患者和非乳腺癌患者的医学数据,包括肿瘤大小、年龄、家族病史等。目标是能够通过这些数据预测一个新来的病人是否患有乳腺癌。但问题是,这些数据多种多样,包括数值、分类等,如何才能准确预测呢…

有监督学习线性回归

1、目标分析&#xff08;回归问题还是分类问题&#xff1f;&#xff09; 2、获取、处理数据 3、创建线性回归模型 4、训练模型 5、模型测试 x_data [[6000, 58], [9000, 77], [11000, 89], [15000, 54]] # 样本特征数据 y_data [30000, 55010, 73542, 63201] # 样本目标数…

Pytorch L1,L2正则化

L1正则化和L2正则化是常用的正则化技术&#xff0c;用于在机器学习模型中控制过拟合。它们的主要区别在于正则化项的形式和对模型参数的影响。 L1正则化&#xff08;Lasso正则化&#xff09;&#xff1a; 正则化项形式&#xff1a;L1正则化使用模型参数的绝对值之和作为正则化…

leetcode_274 H指数

1. 题意 在数组中找到最大的k, 使得至少k个数不小于k。 H指数 2. 题解 2.1 排序 从大到小排序完后&#xff0c;直接模拟即可。 class Solution { public:int hIndex(vector<int>& citations) {sort( citations.begin(), citations.end() );int res 0;int cur …

企业金蝶KIS软件服务器中了locked勒索病毒怎么办,勒索病毒解密

最近一段时间&#xff0c;网络上的locked勒索病毒又开始了新一波的攻击&#xff0c;给企业的正常生产生活带来了严重影响。经过最近一段时间云天数据恢复中心对locked勒索病毒的解密&#xff0c;为大家整理了以下有关locked勒索病毒的相关信息。近期locked勒索病毒主要攻击金蝶…