码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】Number of Islands

时间:2015-07-15 13:18:42      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:图的搜索   island   leetcode   numbers   

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));
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

【LeetCode】Number of Islands

标签:图的搜索   island   leetcode   numbers   

原文地址:http://blog.csdn.net/baidu_22502417/article/details/46890657

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!