【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: 1Example 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的更多相关文章
- 【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 ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- 【leetcode】Number of 1 Bits (easy)
做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- BZOJ.1396.识别子串(后缀自动机/后缀数组 线段树)
题目链接 SAM:能成为识别子串的只有那些|right|=1的节点代表的串. 设这个节点对应原串的右端点为r[i],则如果|right[i]|=1,即\(s[\ [r_i-len_i+1,r_i-le ...
- Activemq+Zookeeper集群
如果在同一台机器上请参考 http://blog.csdn.net/liuyifeng1920/article/details/50233067 http://blog.csdn.net/zuolj/ ...
- 【面试虐菜】—— JAVA面试题(1)
今天参加笔试,里面有设计模式,和一些基础题! 印象最深的是:什么不是Object的函数,我蒙的finalize,哎,无知! 还问了,接口与抽象类的不同,还有多线程的实现方式!下面基本都有. 另外还问了 ...
- windows下用nginx配置https服务器
1.安装nginx 先到nginx官网下在nginx http://nginx.org/en/download.html 将下载好的文件解压出来修改文件名为 nginx ,然后拷贝到C盘下,目录如下: ...
- RabbitMQ高级指南:从配置、使用到高可用集群搭建
本文大纲: 1. RabbitMQ简介 2. RabbitMQ安装与配置 3. C# 如何使用RabbitMQ 4. 几种Exchange模式 5. RPC 远程过程调用 6. RabbitMQ高可用 ...
- 作为互联网人,你必须知道的一些IT类网站
- [Go] Cookie 使用简介
Golang 的 Cookie web 开发免不了要和 cookie 打交道.Go 的 http 库也提供了 cookie 的相关操作. type Cookie struct { Name strin ...
- mixpanel实验教程(1)
一.关于 mixpanel 这个我不想多说,不明确请看官方手冊:https://mixpanel.com/help/reference/ 二.注冊 mixpanel.com 是一个商业机构.它的用户分 ...
- Revit手工创建族
手工创建族 1.画两个参考平面. 图3001 2.点击族类型,添加参数. 图3002,3003 3.添加类型,为类型赋值. 3004 4.创建拉伸截面,完成后,可以三维查看. 3005 5.创建对齐, ...
- lufylegend:文本、鼠标事件、键盘事件
1.文本 <script type="text/javascript">init(50,"mylegend",500,350,main);funct ...