【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 ...
随机推荐
- 【Codeforces666E】Forensic Examination 后缀自动机 + 线段树合并
E. Forensic Examination time limit per test:6 seconds memory limit per test:768 megabytes input:stan ...
- spring-boot 速成(9) druid+mybatis 多数据源及读写分离的处理
按上节继续学习,稍微复杂的业务系统,一般会将数据库按业务拆开,比如产品系统的数据库放在product db中,订单系统的数据库放在order db中...,然后,如果量大了,可能每个库还要考虑做读.写 ...
- [苹果]苹果AppStore应用审核标准
[苹果]苹果AppStore应用审核标准 http://wenku.baidu.com/view/a9152d2c647d27284b7351a1.html 苹果app审核指南 http://we ...
- One-wire Demo on the STM32F4 Discovery Board
One-wire Demo on the STM32F4 Discovery Board Some of the devs at work were struggling to get their s ...
- IAR EWARM __iar_program_start, __iar_data_init3, __iar_copy_init3, __iar_zero_init3
#include <stdint.h> // The type of a pointer into the init table. typedef void const * table_p ...
- 安装oracle环境变量path的值大于1023的解决办法
介绍解决安装oracle安装问题 方法/步骤 安装oracle 10g时遇到环境变量path的值超过1023字符,无法设置该值,如图: 安装oracle 10g时遇到环境变量path的值超 ...
- .Net Discovery 系列之六--深入浅出.Net实时编译机制(下)
接上文 在初始化时,HashTable中各个方法指向的并不是对应的内存入口地址,而是一个JIT预编译代理,这个函数负责将方法编译为本地代码.注意,这里JIT还没有进行编译,只是建立了方法表! 下表(表 ...
- 前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤
在上一篇中实现了增删改查,本篇实现分页和过滤. 本系列包括: 1.前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查2.前端使用AngularJS的$re ...
- SQL Where in list 问题
不过,这种做法有两个缺陷1.Oracle In列表的数目有限制(1000)2.不能复用执行计划,每次几乎都是硬解析.3.In拼接可能存在SQL注入的风险
- require.js 简洁入门
原文地址:http://blog.sae.sina.com.cn/archives/4382 前言 提到require.js大多数人会说提到模块化开发,AMD等等,其实require.js并没有这么多 ...