首页 > 其他分享 >LeetCode 200.岛屿数量

LeetCode 200.岛屿数量

时间:2023-08-18 22:00:54浏览次数:40  
标签:200 int nextn 岛屿 queue 队列 length grid LeetCode

1.题目:

给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。

岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

此外,你可以假设该网格的四条边均被水包围。

 

示例 1:

输入:grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
输出:1

示例 2:

输入:grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
输出:3



2.代码:

dfs:

class Solution {
    public int numIslands(char[][] grid) {
        int num = 0;//计算岛屿的数量
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j]=='1'){//当遇到1的时候
                    num++;//岛屿数量++
                    dfs(grid,i,j);//此时用dfs把整一块岛屿的1变成0
                }
            }
        }
        return num;
    }
    public void dfs(char[][] grid,int i,int j){
        if(i<0 || j<0 || i>=grid.length || j>=grid[0].length || grid[i][j]=='0') return;//递归的出口
        grid[i][j]='0';//实际操作,把1变成0
        dfs(grid,i-1,j);//这就是把水平方向和/或竖直方向上的1变成0
        dfs(grid,i,j-1);
        dfs(grid,i+1,j);
        dfs(grid,i,j+1);
    }
}




bfs:

lass Solution {
    boolean[][] visited ;//标记访问过的节点,不要重复访问
    int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};//表示四个方向
    public int numIslands(char[][] grid) {
        int res = 0;
        visited = 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(!visited[i][j] && grid[i][j]=='1'){//这个条件始终不明白,为什么是true好奇怪
                    bfs(grid,i,j);
                    res++;
                }
            }
        }
        return res;
    }
    public void bfs(char[][] grid,int x,int y){
        Deque<int[]> queue = new ArrayDeque<>();//定义队列
        queue.offer(new int[]{x,y});//起始节点加入队列
        visited[x][y]=true;//只要加入队列,就立即标记为访问过的节点
        while(!queue.isEmpty()){//开始遍历队列里面的元素
            int[] cur = queue.poll();//把队列里的数组元素取出来
            int m = cur[0];
            int n = cur[1];
            for(int i=0;i<move.length;i++){//开始向当前结点的四个方向上下左右遍历
                int nextm = m+move[i][0];
                int nextn = n+move[i][1];//获取四个方向的坐标
                if(nextm<0 || nextn<0 || nextm>=grid.length || nextn>=grid[0].length) continue;//坐标越界了,直接跳过
                if(!visited[nextm][nextn] && grid[nextm][nextn]=='1'){//如果节点没被访问过
                    queue.offer(new int[]{nextm,nextn});//队列添加该节点为下一轮要遍历的节点
                    visited[nextm][nextn] = true;//只要加入队列立即被标记,避免重复访问
                }
            }
        }
    }
}








标签:200,int,nextn,岛屿,queue,队列,length,grid,LeetCode
From: https://blog.51cto.com/u_15806469/7141967

相关文章

  • Leetcode 1388. 3n 块披萨
    (本文只提供了解题思路的思考,原文作者题解连接)先把题目粘贴在这里给你一个披萨,它由3n块不同大小的部分组成,现在你和你的朋友们需要按照如下规则来分披萨:你挑选任意一块披萨。Alice将会挑选你所选择的披萨逆时针方向的下一块披萨。Bob将会挑选你所选择的披萨顺时针方向......
  • leetcode1372dp求交错路径长
    bfd+dpunordered_map<TreeNode*,int>d,p;queue<pair<TreeNode*,TreeNode*>>q;intdp(TreeNode*root){d[root]=p[root]=0;q.push({root,nullptr});while(!q.empty()){autox=q.front();q.pop();autoy=x.second();......
  • [LeetCode][70]climbing-stairs
    ContentYouareclimbingastaircase.Ittakesnstepstoreachthetop.Eachtimeyoucaneitherclimb1or2steps.Inhowmanydistinctwayscanyouclimbtothetop? Example1:Input:n=2Output:2Explanation:Therearetwowaystoclimbtothet......
  • [LeetCode][62]unique-paths
    ContentThereisarobotonanmxngrid.Therobotisinitiallylocatedatthetop-leftcorner(i.e.,grid[0][0]).Therobottriestomovetothebottom-rightcorner(i.e.,grid[m-1][n-1]).Therobotcanonlymoveeitherdownorrightatanypointi......
  • [LeetCode][64]minimum-path-sum
    ContentGivenamxngridfilledwithnon-negativenumbers,findapathfromtoplefttobottomright,whichminimizesthesumofallnumbersalongitspath.Note:Youcanonlymoveeitherdownorrightatanypointintime. Example1:Input:grid=[[......
  • [LeetCode][55]jump-game
    ContentYouaregivenanintegerarraynums.Youareinitiallypositionedatthearray'sfirstindex,andeachelementinthearrayrepresentsyourmaximumjumplengthatthatposition.Returntrueifyoucanreachthelastindex,orfalseotherwise.......
  • [LeetCode][32]longest-valid-parentheses
    ContentGivenastringcontainingjustthecharacters'('and')',returnthelengthofthelongestvalid(well-formed)parenthesessubstring. Example1:Input:s="(()"Output:2Explanation:Thelongestvalidparentheses......
  • [LeetCode][53]maximum-subarray
    ContentGivenanintegerarraynums,findthesubarraywiththelargestsum,andreturnitssum. Example1:Input:nums=[-2,1,-3,4,-1,2,1,-5,4]Output:6Explanation:Thesubarray[4,-1,2,1]hasthelargestsum6.Example2:Input:nums=[1]Output:......
  • [LeetCode][10]regular-expression-matching
    ContentGivenaninputstrings andapatternp,implementregularexpressionmatchingwithsupportfor'.'and'*'where:'.'Matchesanysinglecharacter.​​​​'*'Matcheszeroormoreoftheprecedingelement.T......
  • [LeetCode][42]trapping-rain-water
    ContentGivennnon-negativeintegersrepresentinganelevationmapwherethewidthofeachbaris1,computehowmuchwateritcantrapafterraining. Example1:Input:height=[0,1,0,2,1,0,1,3,2,1,2,1]Output:6Explanation:Theaboveelevationmap......