zoukankan      html  css  js  c++  java
  • 【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));
        }
    
    }
    
  • 相关阅读:
    【题解】「CF1373B」01 Game
    asdfasd
    android开发DialogFragment禁止按back键消失的解决方法
    MySQL报错1055
    IDEA中使用Git拉取代码时报 Git pull failed原因及处理方法
    数据挖掘导论 完整版+PPT+Python R代码
    MATLAB统计分析与应用 40个案例分析[源代码及数据]
    机器学习实战 中英文版
    电力系统负荷预测数据集【全】含下载链接
    Nginx 导致swagger setCookie sessionid 失效
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8945485.html
Copyright © 2011-2022 走看看