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));
        }
    
    }
    
  • 相关阅读:
    如何使用PhoneGap打包Web App
    js获取鼠标点击事件的相对位置
    IDEA跳转到上一个下一个方法的快捷键
    JunitGenerator
    无所不能的PowerMock,mock私有方法,静态方法,测试私有方法,final类
    IntelliJ IDEA 2019.3注册码(亲测有效,可激活至 2089 年)
    NACOS MalformedInputException 无法读取中文配置问题
    maven maven-surefire-plugin的乱码问题
    Idea单元测试Junit Generator设置
    Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8945485.html
Copyright © 2011-2022 走看看