【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 ...
随机推荐
- [TC14088]LimitedMemorySeries1
[TC14088]LimitedMemorySeries1 题目大意: 给定长度为\(n(n\le5\times10^6)\)的数组\(X\),询问不超过\(q(q\le100)\)次,每次询问第\( ...
- 使用WindowsAPICodePack实现翻译功能
仅限于以下几种语言间的翻译: 在我的另一篇博文<图片批量压缩>中,有介绍WindowsAPICodePack库,该库是微软提供的一套基于Win7及以上版本操作系统的系统库,可以帮助我们完成 ...
- 使用Puppeteer进行数据抓取(一)——安装和使用
Puppeteer是 Google Chrome 团队官方的Chrome 自动化工具.它本身是基于Chrome Dev Protocol协议实现的,但它提供了更高层次API封装,使用起来更加方便快捷. ...
- .net项目中使用Quartz
(1)在web.config中进行相关配置 <configSections> <section name="quartz" type="System.C ...
- 清除数据库表、外键、存储过程SQL
1.删除所有外键 ) begin exec(@c1) fetchnextfrom c1 into@c1 endclose c1deallocate c1 2. ...
- Silverlight for Windows Phone Toolkit
Silverlight Toolkit 是一套codeplex上的很好的代码库,它里面包含了很多常用的但是Silverlight本身并不包含的控件.文档等内容.现在Silverlight Toolki ...
- AndroidStudio工具将Module项目导出成Jar和arr库
原文:http://blog.csdn.net/liulei823581722/article/details/52919697 该篇首先讲述利用AndroidStudio如何把一个module项目导 ...
- Linux tar包相关命令
tar [-j|-z][cv][-f 新建的文件名] filename... <==打包与压缩 tar [-j|-z][tv][-f 新建的文件名] <==查看文件名 tar [-j| ...
- eclipse 中 import sun.misc.BASE64Decoder; 报错
from://http://blog.sina.com.cn/s/blog_48964b120101ahrf.html 在android做3DES加密功能时 eclipse 中 import sun. ...
- 好汉两个半第一季/全集Two And A Half Men迅雷下载
第一季 Two and a Half Men Season 1 (2003)看点:Charlie是一个潇洒自由的单身汉,但正面临离婚危机的兄弟Alan带着儿子Jake的突然来访完全打乱了Charlie ...