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));
        }
    
    }
    
  • 相关阅读:
    解决VUE刷新或者加载出现闪烁
    解决VUE<router-link>不能触发点击事件
    H5的本地存储web Storage
    格式化数字格式
    移动终端浏览器版本信息
    新的开始
    PHP用PHPExcel导入Excel表格的数据到MySQL(thinkPHP3.2.3)
    Layui的分页模块在网站中的应用
    PHPstorm连接ftp
    自定义PHPstorm快捷键
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8945485.html
Copyright © 2011-2022 走看看