Number of Islands——LeetCode
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
哈哈,这是今天的新题,十多分钟A了,题目不难,我的做法是BFS,题目大意是1代表岛屿,0代表水,可以假设这个grid周围都是水,岛屿定义是四周都是水,求岛屿的个数。
我的做法是,从第一个开始,如果是1,并且没有访问过visit为false,那么加入queue里,当queue非空取出来并加入它上下左右的邻居,并且把这些节点visit设置为true,时间复杂度是O(M*N),空间复杂度的话,因为同时在queue里的最多也就4个节点的坐标,所以是O(1)。
Talk is cheap>>
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int rowLen = grid.length;
int colLen = grid[0].length;
boolean[][] visited = new boolean[rowLen][colLen];
int[][] direct = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
Deque<Integer> queue = new ArrayDeque<>();
int res = 0;
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
if (!visited[i][j] && grid[i][j] == '1') {
res++;
queue.add(i * colLen + j);
while (!queue.isEmpty()) {
int pos = queue.poll();
int curr_x = pos / colLen;
int curr_y = pos % colLen;
if (visited[curr_x][curr_y]) {
continue;
}
visited[curr_x][curr_y] = true;
for (int k = 0; k < 4; k++) {
int x = curr_x + direct[k][0];
int y = curr_y + direct[k][1];
if (x >= 0 && y >= 0 && x < rowLen && y < colLen && grid[x][y] == '1') {
queue.add(x * colLen + y);
}
}
}
}
}
}
return res;
}
Number of Islands——LeetCode的更多相关文章
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 200 Number of Islands
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 Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- Java 下实现锁无关数据结构--转载
介绍 通常在一个多线程环境下,我们需要共享某些数据,但为了避免竞争条件引致数据出现不一致的情况,某些代码段需要变成原子操作去执行.这时,我们便需要利用各种同步机制如互斥(Mutex)去为这些代码段加锁 ...
- Creating a simple static file server with Rewrite--reference
Today, I’d like to take a quick moment to demonstrate how to make a simple file server using Rewrite ...
- [转] git reset简介
http://blog.csdn.net/hudashi/article/details/7664464 http://guibin.iteye.com/blog/1014369 http://hi. ...
- Android Studio学习随笔-移动动画的实现
在上一篇博客我已经讲述了三种事件的实现方法,而现在我用复用方法来实现控件的自动移动,当然要实现控件的移动,先得在activity_main.xml文件中放置一个控件,此处我放置的是一个button控件 ...
- Verilog之event的用法
编写verilog的testbench时,可使用event变量触发事件. event变量声明为: event var; event触发为: ->var; 捕获触发为: @(var); 在mode ...
- 【开源java游戏框架libgdx专题】-13-开发工具-地图的使用
支持libGDX的地图编辑器有很多种,其中比较常用的工具为Tiled地图工具.Tiled是一款非常好用的地图编辑器.下载地址:http://www.mapeditor.org TiledMap类: 又 ...
- shijan
1.<?php 2. $zero1=date(“y-m-d h:i:s”); 3. $zero2=”2010-11-29 21:07:00′; 4. echo “zero1的时间为:”.$zer ...
- leetcode修炼之路——350. Intersection of Two Arrays II
题目如下: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2 ...
- 99%Bug 修复方法
以下仅支持4.0.3或之后的设备 (写在前面,这个教程需要安装两个app,且卸载后无法达到效果,所以有app drawer洁癖者慎重(你可以把它们隐藏起来么). 当然等官方rom更新或者安装最新三方r ...
- MySQL 查询数据
MySQL 查询数据 MySQL 数据库使用SQL SELECT语句来查询数据. 你可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MyS ...