LeetCode – Number of Islands
Given a -d grid map of ''s (land) and ''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 :
The basic idea of the following solution is merging adjacent lands, and the merging should be done recursively.
1. DFS
public int numIslands(char[][] grid) {
if(grid==null || grid.length==0||grid[0].length==0)
return 0; int m = grid.length;
int n = grid[0].length; int count=0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(grid[i][j]=='1'){
count++;
merge(grid, i, j);
}
}
} return count;
} public void merge(char[][] grid, int i, int j){
int m=grid.length;
int n=grid[0].length; if(i<0||i>=m||j<0||j>=n||grid[i][j]!='1')
return; grid[i][j]='X'; merge(grid, i-1, j);
merge(grid, i+1, j);
merge(grid, i, j-1);
merge(grid, i, j+1);
}
二刷:
注意在变‘1’为‘2’的时候要判断当前值是否为‘1’用来减少memory消耗
class Solution {
public int numIslands(char[][] grid) {
int row = grid.length;
if(row == 0){
return 0;
}
int col = grid[0].length;
int count = 0;
Queue<Integer[]> queue = new LinkedList<>();
for(int i=0; i<row; i++){
for(int j = 0; j<col; j++){
if(grid[i][j] == '1'){
count++;
Integer[] indexs = {i,j};
queue.offer(indexs);
while(!queue.isEmpty()){
Integer[] temp = queue.poll();
int m = temp[0]; int n = temp[1];
if(grid[m][n] == '1'){
grid[m][n]='2';
if(m-1 >= 0 && grid[m-1][n]=='1'){
Integer[] index = {m-1,n};
queue.offer(index);
}
if(m+1<row&&grid[m+1][n]=='1'){
Integer[] index = {m+1,n};
queue.offer(index);
}
if(n-1>=0&&grid[m][n-1]=='1'){
Integer[] index = {m,n-1};
queue.offer(index);
}
if(n+1<col&&grid[m][n+1]=='1'){
Integer[] index = {m,n+1};
queue.offer(index);
}
}
}
}
}
}
return count;
}
}
LeetCode – Number of Islands的更多相关文章
- [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 ...
- 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] Number of Islands II
Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...
- [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. ...
- 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 岛的数量(DFS,BFS)
题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...
- 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 Distinct Islands 不同岛屿的个数
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
随机推荐
- unity中手机触摸代码
#elif UNITY_IOS || UNITY_ANDROID if(Input.touchCount <= 0) { return;/ ...
- window.open()打开页面
一.window.open()支持环境:JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法:window.open(pageURL,name,pa ...
- cocos2d方块方块
cGridSize=32 cSceneWidth=8+2 cSceneHeight=18 fuction Grid2Pos(x,y) local visibleSize=cc.Director:get ...
- day22-python操作mysql2
数据库连接池 python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一定数量时, ...
- 写一个xml文件到磁盘的方法
/** * 往磁盘上写一个xml文件 * * <?xml version="1.0" encoding="UTF-8" standalone=" ...
- 平面图转对偶图&19_03_21校内训练 [Everfeel]
对于每个平面图,都有唯一一个对偶图与之对应.若G‘是平面图G的对偶图,则满足: G'中每一条边的两个节点对应着G中有公共边的面,包括最外部无限大的面. 直观地讲,红色标出来的图就是蓝色标出的图的对偶图 ...
- tidb使用坑记录
转载自:https://www.cnblogs.com/linn/p/8459327.html tidb使用坑记录 1.对硬盘要求很高,没上SSD硬盘的不建议使用 2.不支持分区,删除数据是个大坑. ...
- urllib 获取页面或发送信息
#! /usr/bin/env python3 # -*- coding:utf-8 -*- #urllib提供了一系列用于操作URL的功能. #urllib的request模块可以非常方便地抓取UR ...
- node(3)Buffer缓冲区
buffer 专门用来存放二进制数据的缓冲区:处理文件流 TCP流 const buf = Buffer.from('runoob', 'ascii'); // 创建一个长度为 10.且用 0x1 填 ...
- 201621123001 《Java程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. Mysql数据库简单操作,常用的操作命令 启动:进入Mysql (从命令行mysql -u root -p) 退 ...