【LeetCode】Number of Islands

Number of Islands

问题描述

Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count 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.

Example 1:
11110
11010
11000
00000
Answer: 1

Example 2:
11000
11000
00100
00011
Answer: 3

算法思想

问题类似于图的遍历问题,我们假设每一个点都是顶点,利用图的优先搜索算法,进行遍历。
1. 每次选择一个点,并且访问这个点的4上下左右四个方向上的点,直到访问完所有的临接点。
2. 每次重新选择点就是一个新的岛的存在。

算法实现

public class Solution {
    boolean[][] visitPoint = null;
    int cols=0,rows=0;
    public void visitIsland(char[][] grid, int i, int j) {
        visitPoint[i][j]=true;
        if(i-1>=0&&visitPoint[i-1][j]==false&&grid[i-1][j]=='1')
            visitIsland(grid,i-1,j);
        if(i+1<rows&&visitPoint[i+1][j]==false&&grid[i+1][j]=='1')
            visitIsland(grid,i+1,j);
        if(j-1>=0&&visitPoint[i][j-1]==false&&grid[i][j-1]=='1')
            visitIsland(grid,i,j-1);
        if(j+1<cols&&visitPoint[i][j+1]==false&&grid[i][j+1]=='1')
            visitIsland(grid,i,j+1);
    }

    public int numIslands(char[][] grid) {
        if(grid==null)
            return 0;

        rows = grid.length;
        if(rows==0)
            return 0;

        cols = grid[0].length;

        visitPoint = new boolean[rows][cols];
        for(int i=0;i<rows;i++){
            for(int j=0;j<cols;j++){
                visitPoint[i][j]=false;
            }
        }
        int num = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (visitPoint[i][j] == false && grid[i][j] == '1') {
                    num++;
                    visitIsland(grid, i, j);
                }
            }

        }
        return num;
    }
}

算法时间

T(n)=O(n2)

演示结果

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SolutionTest {
    private Solution s = new Solution();

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testNumIslands() {
        char[][] grid3 = { 
                { '1', '1', '0', '0', '0' }, 
                { '1', '1', '0', '0', '0'},
                { '0', '0', '1', '0', '0' }, 
                { '0', '0', '0', '1', '1'} 
        };
        assertEquals(3,s.numIslands(grid3));

        char[][] grid1 = { 
                { '1', '1', '1', '1', '0' }, 
                { '1', '1', '0', '1', '0'},
                { '1', '1', '0', '0', '0' }, 
                { '0', '0', '0', '0', '0'} 
        };
        assertEquals(1,s.numIslands(grid1));

        char[][] grid = {};
        assertEquals(0,s.numIslands(grid));
        assertEquals(0,s.numIslands(null));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值