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 ...
随机推荐
- Struts2 Spring hibernate 整合示例 .
示例工具:MyEclipse 8.5.Tomcat 6.0.MySql 步骤: 1.创建一个WEB工程,命名为BookShop(名字自己取,此处为示例工程名): 2.导入struts2的核心jar包, ...
- [转] boost.circular_buffer简介
http://www.cnblogs.com/TianFang/archive/2013/02/05/2892503.html 很多时候,我们需要在内存中记录最近一段时间的数据,如操作记录等.由于这部 ...
- 使用搬瓦工搭建javaweb环境
/* 本文是基于搬瓦工vps的centos-6-x86_64的Linux系统搭建. 需准备的工具:1.putty(用于连接Linux系统) 2.WinSCP(搬瓦工官方提供的ftp上传下载工 ...
- Python之路,Day22 - 网站用户访问质量分析监测分析项目开发
Python之路,Day22 - 网站用户访问质量分析监测分析项目开发 做此项目前请先阅读 http://3060674.blog.51cto.com/3050674/1439129 项目实战之 ...
- Android开发手记(23) Notification
有时候,我们需要应用程序在状态内显示一些通知信息,这时我们就需要使用Notification来完成这一工作.也许我们会想到以前经常使用的Toast来通知用户.虽然Notification与Toast都 ...
- oracle解析xml完成版
SELECT * FROM XMLTABLE('$B/DEAL_BASIC/USER_DEAL_INFO' PASSING XMLTYPE('<?xml version="1.0&qu ...
- Hive学习之六 《Hive进阶— —hive jdbc》 详解
接Hive学习五 http://www.cnblogs.com/invban/p/5331159.html 一.配置环境变量 hive jdbc的开发,在开发环境中,配置Java环境变量 修改/etc ...
- TalkingData Cocos2dx集成指南【最新】
续:最近终于腾出时间把TalkingData的Cocos版本好好折腾一下了,总感觉之前的各个版本在集成上都很蹩脚.给广大开发者带了很多困扰...“游戏正着急上线呢,哪还有时间去仔细看TalkingDa ...
- 巧用Session Manager还原Firefox丢失会话
今天Firefox Crash之后,我的会话全部丢失了.按照以往来说,Firefox在重新启动之后或者Crash之后会有一个会话还原的页面.但今天确实没有.后来我进行Google查阅,试了很多种办法. ...
- jQuery获取select、checkbox、radio的方法总结
在进行网页的前后台交互的时候,经常需要获取单选框(radio).复选框(checkbox)以及下拉列表(select/dropdownlist)选中的值. 总结了一下,方便以后复习查看: 1.获取选中 ...