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 ...
随机推荐
- 那些年不错的Android开源项目(转)
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- 怎样找Android里自带的数据库?
首先先让它显示出来. 窗口window下的Show View 下的File Explorer(other里), 这时File Explorer窗口 就会显示出来,下一步我们找到data下的data,然 ...
- Template Method 模板方法
简介 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. 模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤的细节 抽象模板AbstractClass的方法分为两类: 基本 ...
- Activity 【生命周期】
不同情况下的回调 我们打开应用时先后调用了onCreate()->onStart()->onResume 当我们按BACK键时,我们这个应用程序将结束,这时候我们将先后调用onPause( ...
- HTML5音乐播放器
//HTML5部分 <a href="javascript:void(0);" onclick="getCurrentTime('firefox');"& ...
- PHP解析xml
<?xml version="1.0" encoding="UTF-8"?> <ZIP_result> <result name= ...
- (转) dedecms中自定义数据模型
刚学习完dedecms的标签语法,我有很多困惑,觉得标签的意义比较抽象,不知道如何用标签来写一些具体的内容.如果有一些数据库的编程经验,就知道一个很常用的编程范例—增删改查.比如说,我要建立的是书本的 ...
- Android系统下的动态Dex加载与app速度优化
1 问题 在Android系统中,一个App的所有代码都在一个Dex文件里面.Dex是一个类似Jar的存储了多有Java编译字节码的归档文件.因为Android系统使用Dalvik虚拟机,所以需要把 ...
- MySQL 序列使用
MySQL 序列使用 MySQL序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 本章我们将介绍如 ...
- uva 1378 A Funny Stone Game (博弈-SG)
题目链接:http://vjudge.net/problem/viewProblem.action?id=41555 把第i堆的每个石子看出一堆个数为n-i的石子,转换为组合游戏 #include & ...