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 ...
随机推荐
- Python之路,Day22 - 网站用户访问质量分析监测分析项目开发
Python之路,Day22 - 网站用户访问质量分析监测分析项目开发 做此项目前请先阅读 http://3060674.blog.51cto.com/3050674/1439129 项目实战之 ...
- Android 项目建立步骤
使用eclipse,进行安卓开发,在建立项目的时候,有些步骤必须注意的, 本文就是对使用eclipse进行android开发的简单说明: 一.模拟器配置设定 使用eclipse开发安卓,需要用到and ...
- Activity 【生命周期】
不同情况下的回调 我们打开应用时先后调用了onCreate()->onStart()->onResume 当我们按BACK键时,我们这个应用程序将结束,这时候我们将先后调用onPause( ...
- 没有懂的leetcode
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Unicode 与多字节编码
int _tmain(int argc, _TCHAR* argv[]) { //定义LPWSTR 类型的宽字符串 LPWSTR szUnicode = L"This is a Unicod ...
- cancel-ng-swipe-right-on-child
<!DOCTYPE html> <html lang="en" ng-app="myapp"> <head> <met ...
- PHP 网页爬虫
只能爬一个页面 <?php function get_urls($url){ $url_array=array(); $the_first_content=file_get_contents($ ...
- eclipse[日文版] 的SVN 上传步骤
可能有些朋友在日企上班,肯定要用到SVN,可是一般就下载和更新,没有用到上传 这里来介绍下上传 1.项目右键 2.点击Share Project 3.点击SVN下一步 4.选择你的上传的服务器地址 5 ...
- (转)如何将ecshop首页主广告位的flash轮播替换为js轮播
转之--http://www.ecshoptemplate.com/article-1710.html 这个ecshop很常见,因为现在比起flash难以修改,js更加符合人们的使用习惯,而默认ecs ...
- POJ 2411.Mondriaan's Dream 解题报告
题意: 给出n*m (1≤n.m≤11)的方格棋盘,用1*2的长方形骨牌不重叠地覆盖这个棋盘,求覆盖满的方案数. Solution: 位运算+状态压缩+dp ...