#200 Number of Islands

Description

Given an m x n 2D binary grid grid which represents a map of '1’s (land) and '0’s (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Examples

Example 1:

Input: grid = [
[“1”,“1”,“1”,“1”,“0”],
[“1”,“1”,“0”,“1”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“0”,“0”,“0”]
]
Output: 1

Example 2:

Input: grid = [
[“1”,“1”,“0”,“0”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“1”,“0”,“0”],
[“0”,“0”,“0”,“1”,“1”]
]
Output: 3

Constraints:

m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] is ‘0’ or ‘1’.

思路

感觉就是一个深度优先搜索的问题,一开始时候有点钻牛角尖了,觉得要往一个方向走完了再去别的方向,后来想了一下,觉得递归的流程里面其实已经包含了相关的判断了,我再写在循环里面只会增加比较和运算次数,所以总结如下两个代码

代码

class Solution {
    char[][] matrix;
    
    public void setZeros(int x, int y){
        int max_x = matrix.length;
        int max_y = matrix[0].length;
        if (x >= max_x || y >= max_y || x < 0 || y < 0)
            return;
        
        int tmp_x = x + 1, tmp_y = y;
        while(tmp_x < max_x && matrix[tmp_x][tmp_y] == '1') {
            matrix[tmp_x][tmp_y] = '0';
            setZeros(tmp_x, tmp_y);
            tmp_x ++;
        }
        
        tmp_x = x - 1;
        while(tmp_x >= 0 && matrix[tmp_x][tmp_y] == '1') {
            matrix[tmp_x][tmp_y] = '0';
            setZeros(tmp_x, tmp_y);
            tmp_x --;
        }
        
        tmp_x = x;
        tmp_y = y + 1;
        while(tmp_y < max_y && matrix[tmp_x][tmp_y] == '1') {
            matrix[tmp_x][tmp_y] = '0';
            setZeros(tmp_x, tmp_y);
            tmp_y ++;
        }
        
        tmp_y = y - 1;
        while(tmp_y >= 0 && matrix[tmp_x][tmp_y] == '1') {
            matrix[tmp_x][tmp_y] = '0';
            setZeros(tmp_x, tmp_y);
            tmp_y --;
        }
    }
    
    public int numIslands(char[][] grid) {
        int count = 0;
        matrix = new char[grid.length][grid[0].length];
        
        for (int i = 0; i < grid.length; i++){
            for (int j = 0; j < grid[0].length; j++){
                matrix[i][j] = grid[i][j];
            }
        } 
        
        for (int i = 0; i < grid.length; i++){
            for (int j = 0; j < grid[0].length; j++){
                if (matrix[i][j] == '1'){
                    matrix[i][j] = '0';
                    setZeros(i, j);
                    count ++;
                }
            }
        } 
        
        return count;
    }
}
class Solution {
    char[][] matrix;
    
    public void setZeros(int x, int y){
        int max_x = matrix.length;
        int max_y = matrix[0].length;
        if (x >= max_x || y >= max_y || x < 0 || y < 0 || matrix[x][y] != '1' )
            return;
        
        matrix[x][y] = '0';
        setZeros(x + 1, y);
        setZeros(x - 1, y);
        setZeros(x, y + 1);
        setZeros(x, y - 1);
        
    }
    
    public int numIslands(char[][] grid) {
        int count = 0;
        matrix = new char[grid.length][grid[0].length];
        
        for (int i = 0; i < grid.length; i++){
            for (int j = 0; j < grid[0].length; j++){
                matrix[i][j] = grid[i][j];
            }
        } 
        
        for (int i = 0; i < grid.length; i++){
            for (int j = 0; j < grid[0].length; j++){
                if (matrix[i][j] == '1'){
                    setZeros(i, j);
                    count ++;
                }
            }
        } 
        
        return count;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值