首页 > 其他分享 >LeetCode 1020.飞地的数量

LeetCode 1020.飞地的数量

时间:2023-08-19 15:33:39浏览次数:32  
标签:count 1020 offer 飞地 单元格 queue int grid LeetCode

1.题目:

给你一个大小为 m x n 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。

一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。

返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。

 

示例 1:

LeetCode 1020.飞地的数量_i++

输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
输出:3
解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。

示例 2:

LeetCode 1020.飞地的数量_i++_02

输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
输出:0
解释:所有 1 都在边界上或可以到达边界。



2.代码:

dfs:

class Solution {
    int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};
    int count=0;
    public int numEnclaves(int[][] grid) {
        for(int i=0;i<grid.length;i++){
            if(grid[i][0]==1){
                dfs(grid,i,0);
            }
            if(grid[i][grid[0].length-1]==1){
                dfs(grid,i,grid[0].length-1);
            }
        }
        for(int j=1;j<grid[0].length-1;j++){
            if(grid[0][j]==1){
                dfs(grid,0,j);
            }
            if(grid[grid.length-1][j]==1){
                dfs(grid,grid.length-1,j);
            }
        }
        count=0;
        for(int i=1;i<grid.length-1;i++){
            for(int j=1;j<grid[0].length-1;j++){
                if(grid[i][j]==1){
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }
    public void dfs(int[][] grid,int x,int y){
        if(grid[x][y]==0) return;
        grid[x][y]=0;
        count++;
        for(int i=0;i<move.length;i++){
            int m=x+move[i][0];
            int n=y+move[i][1];
            if(m<0||n<0||m>=grid.length||n>=grid[0].length||grid[m][n]==0) continue;
            dfs(grid,m,n);
        }
    }
}


bfs:

class Solution {
    int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};
    int count=0;
    public int numEnclaves(int[][] grid) {
        for(int i=0;i<grid.length;i++){//把左右两边的1变成0
            if(grid[i][0]==1){
                bfs(grid,i,0);
            }
            if(grid[i][grid[0].length-1]==1){//注意这里的宽度是grid[0].length
                bfs(grid,i,grid[0].length-1);
            }
        }
        for(int j=1;j<grid[0].length-1;j++){//把上下两边的1变成0 注意之前重复的不用变了
            if(grid[0][j]==1){
                bfs(grid,0,j);
            }
            if(grid[grid.length-1][j]==1){//注意这里的是长度grid.length-1
                bfs(grid,grid.length-1,j);
            }
        }
        count=0;//计算的也算是总面积
        for(int i=1;i<grid.length-1;i++){
            for(int j=1;j<grid[0].length-1;j++){
                if(grid[i][j]==1){
                    bfs(grid,i,j);
                }
            }
        }
        return count;
    }
    public void bfs(int[][] grid,int x,int y){
       Queue<Integer> queue = new LinkedList<>();
       queue.offer(x);
       queue.offer(y);
       count++;
       grid[x][y]=0;
       while(!queue.isEmpty()){//注意这个判断条件不要忘记了
            int m1 = queue.poll();
           int n1=queue.poll();
 for(int i=0;i<move.length;i++){
          int m=m1+move[i][0];//注意这里要重新用变量,不要直接用抛出来的
           int n=n1+move[i][1];
           if(n<0||m<0||m>=grid.length||n>=grid[0].length||grid[m][n]==0) continue;
          if(grid[m][n]==1){
              queue.offer(m);
              queue.offer(n);
              count++;
              grid[m][n]=0;
          }
       }
       }
      
    }
}









标签:count,1020,offer,飞地,单元格,queue,int,grid,LeetCode
From: https://blog.51cto.com/u_15806469/7150139

相关文章

  • 【LeetCode1384. 按年度列出销售总额】MySQL使用with recursive根据开始日期和结束日
    题目地址https://leetcode.cn/problems/total-sales-amount-by-year/description/代码WITHRECURSIVEDateSeriesAS(SELECTproduct_id,period_startASsale_date,period_end,average_daily_salesFROMSales--Assumingyourtablenameissales_dataUN......
  • 【LeetCode2199. 找到每篇文章的主题】字符串处理题,使用MySQL里的group_concat和LOCAT
    题目地址https://leetcode.cn/problems/finding-the-topic-of-each-post/description/代码witht1as(selectp.*,k.*fromPostspleftjoinKeywordskonLOCATE(LOWER(CONCAT('',word,'')),LOWER(CONCAT('',conte......
  • Leetcode 142. 环形链表II(Linked list cycle ii)
    题目链接给定一个链表的头节点head,返回链表开始入环的第一个节点。.如果链表无环,则返回null.如果链表中有某个节点,可以通过连续跟踪next指针再次到达,则链表中存在环.为了表示给定链表中的环,评测系统内部使用整数pos来表示链表尾连接到链表中的位置(索引从0开始).......
  • 【LeetCode2118. 建立方程】 group_concat指定分隔符,指定排序顺序
    目录题目地址题目描述代码题目地址https://leetcode.cn/problems/build-the-equation/description/题目描述Example2:输入:Terms表:+-------+--------+|power|factor|+-------+--------+|4|-4||2|1||1|-1|+-------+---......
  • 【LeetCode1225. 报告系统状态的连续日期】MySQL使用lag,lead得到连续段的:开始标志,结束
    目录题目地址题目描述代码题目地址https://leetcode.cn/problems/report-contiguous-dates/description/题目描述Asystemisrunningonetaskeveryday.Everytaskisindependentoftheprevioustasks.Thetaskscanfailorsucceed.Writeasolution toreportth......
  • 【LeetCode173. 最多连胜的次数】MySQL用户变量编程解法
    目录题目地址题目描述代码题目地址https://leetcode.cn/problems/longest-winning-streak/description/题目描述选手的 连胜数是指连续获胜的次数,且没有被平局或输球中断。编写解决方案来计算每个参赛选手最多的连胜数。结果可以以任何顺序返回。代码WITHt1AS(......
  • 【LeetCode1454. 活跃用户】MySQL 用户自定义变量,面向过程编程解决"连续天数"的问题
    目录题目地址题目描述代码题目地址https://leetcode.cn/problems/active-users/description/题目描述活跃用户是指那些至少连续 5天登录账户的用户。编写解决方案, 找到活跃用户的id和name。返回的结果表按照id排序 。代码注意需要处理,同一天多次登录的情形......
  • leetcode刷题日记
    unordered_set散列哈希表:C++STLunordered_set容器完全攻略(biancheng.net)unordered_map:详细介绍C++STL:unordered_map-朤尧-博客园(cnblogs.com)DAY1数组217.存在重复元素难度简单629给你一个整数数组nums。如果任一值在数组中出现至少两次,返回true;如果数组......
  • Leetcode 160. 链表相交(Intersection of two linked lists lcci)
    题目链接给定两个单链表的头节点headA和headB,请找出并返回两个单链表相交的起始节点.如果两个链表没有交点,返回null.图示两个链表在节点c1开始相交,题目数据保证整个链式结构中不存在环.示例1:输入:intersectVal=8,listA=[4,1,8,4,5],listB=[5,0,1,8,4,5],sk......
  • LeetCode 200.岛屿数量
    1.题目:给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。此外,你可以假设该网格的四条边均被水包围。 示例1:输入:grid=[["1","1","1","1","0"],["1","1",&q......