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. IBM MR10i阵列卡配置Raid0/Raid1/Raid5(转)

    RAID5配置: 其实RAID0/RAID1都基本一致,只是选择的类型不同. 1. 开机看到ctrl+h的提示按下相应的键,等ServerRaid 10-i卡初始化完成则进入WebBIOS 配置界面: ...

  2. css卷叶效果

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

  3. GEF最简单的入门-helloword(1)

    最近做插件项目.主要负责GEF这块. 好吧.资料真少的可以.特别是入门.都是一大堆一大堆的.网上最火的八进制的文章但对于我这种菜鸟级别看了还是一头雾水.各种资料折腾了半天.终于折腾出一个真正的入门例子 ...

  4. [Winform]无边框窗口悬浮右下角并可以拖拽移动

    摘要 简单实现了一个这样的功能,程序启动时,窗口悬固定在右下角,并可以通过鼠标拖拽移动. 核心代码块 无边框窗口并不出现在任务栏 //无边框 this.FormBorderStyle = System ...

  5. WordPress主题开发:实现分页功能

    注意的是这个受后台这里的文章篇数设置所影响~ 一.使用内置方法 (WordPress 4.1以前无效) the_posts_pagination 输出分页式导航,用法: <?php the_po ...

  6. LinkedList、Stack、Queue、PriorityQueue的总结

    1.这几种容器的特点 都是按照顺序来存储元素. 取元素的时候的不同点: LinkedList:按照下标随意取元素 Stack:后进先出取元素 Queue:先进先出取元素 PriorityQueue:按 ...

  7. SharePoint 设置客户端上传文件大小

    in sharepoint 2013, 2016 , there is a limitatoin on the size of the uploading files , default size f ...

  8. Android之仿iphone抖动效果

    转自:http://blog.csdn.net/long33long/article/details/7693671 布局文件: <?xml version="1.0" en ...

  9. c# action<> func<> 这2个委托怎么用和理解

    其实很好理解的呢!~首先你需要明白,他们是委托的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 1.1定义一个委托: 比如,我们原来写委托: public de ...

  10. 使用Python读取Mp3的标签信息

    什么是ID3 MP3是音频文件最流行的格式,它的全称是 MPEG layer III.但是这种格式不支持对于音频内容的描述信息,包括歌曲名称.演唱者.专辑等等. 因此在1996年,Eric Kemp在 ...