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的更多相关文章

  1. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  3. 【leetcode】Number of 1 Bits (easy)

    做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };

  4. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  5. 【LeetCode】并查集 union-find(共16题)

    链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给 ...

  6. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. android 获取系统默认路径

    Environment.getDataDirectory().getPath() : /dataEnvironment.getDownloadCacheDirectory().getPath()  : ...

  2. hdu 4442 37届金华赛区 A题

    题意:给出一些队伍,每个队伍有初始等待时间和每秒增加的时间,求最短时间 假设有两个队初始时间和每秒增加时间为a1,b1和a2,b2 若第选择第一个的时间小于第二个,则 a1+a2+a1*b2<a ...

  3. css实现背景图片模糊

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 在mysql中使用group by和order by取每个分组中日期最大一行数据

    转载自:https://blog.csdn.net/shiyong1949/article/details/78482737 在mysql中使用group by进行分组后取某一列的最大值,我们可以直接 ...

  5. 异步接收MSMQ消息

    在这部分,我们将使用ThreadPool 和MSMQ 进行消息收发.MSMQ 是一个分布式队列,通过MSMQ 一个应用程序可以异步地与另外一个应用程序通信. 在一个典型的场景中,我们要向维护一个队列的 ...

  6. 「GIT SourceTree冲突」解决方案

    现在程序猿标配GIT作为代码管理,但是从SVN到GIT学习中,其中GIT的冲突是一个难点,常常会导致Push不上去,Pull不下来,很尴尬的地步,还不知道自己写的代码被覆盖没,废话不多说,直接上干货! ...

  7. List、Set、Map 和 Queue 之间的区别

    list 和set 有共同的父类 它们的用法也是一样的 唯一的不太就是set中不能有相同的元素 list中可以list和set的用途非常广泛 list可以完全代替数组来使用map 是独立的合集 它使用 ...

  8. 利用Android Lost通过互联网或短信远程控制安卓设备

    利用Android Lost通过互联网或短信远程控制安卓设备 作者:Jack Wallen| 杰克·瓦伦翻译:PurpleEndurer.2014-11-15第1版 使用智能手机要考虑的一个至关重要的 ...

  9. 委托, 泛型委托,Func<T>和Action<T>

    使用委托来做一些事情,大致思路是: 1.定义声明一个委托,规定输入参数和输出类型.2.写几个符合委托定义的方法.3.把方法列表赋值给委托4.执行委托 internal delegate int MyD ...

  10. (转)java并发编程--Executor框架

    本文转自https://www.cnblogs.com/MOBIN/p/5436482.html java并发编程--Executor框架 只要用到线程,就可以使用executor.,在开发中如果需要 ...