47.Number of Islands(岛的数量)
Level:
Medium
题目描述:
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:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
思路分析:
寻找岛屿的数量,如果遇到一个1,四周都是0,那么为一个岛,只要水平或者垂直临近的1都算是1个岛,求一个二维数组中岛的个数。即求数组中不相邻的1的个数,解决思路,遍历数组,碰到一个1,就把周围所有相连的1都标记为非1,这样整个遍历过程中碰到的1的个数就是所求的解。
代码:
public class Solution{
public int numIslands(char [][]grid){
if(grid==null||grid.length==0)
return 0;
int res=0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]!='1')
continue;
res++;
dfs(grid,i,j);
}
}
return res;
}
public void dfs(char [][]grid,int i,int j){
if(i<0||i==grid.length||j<0||j==grid[0].length)
return;
if(grid[i][j]=='1'){
grid[i][j]='0';
dfs(grid,i+1,j);
dfs(grid,i,j+1);
dfs(grid,i-1,j);
dfs(grid,i,j-1);
}
}
}
47.Number of Islands(岛的数量)的更多相关文章
- LeetCode Number of Islands 岛的数量(DFS,BFS)
题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...
- [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] 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 ...
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- [LeetCode] 305. Number of Islands II 岛屿的数量 II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...
- [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] 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 中判断字符串相等
今天写Java代码时遇到一个问题,就是关于判断两个字符串是否相等的问题.(刚尝试用SSH框架) 在大多编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的 ...
- 萌新java入门笔记
首先声明以下内容只是散乱笔记,如果有误还望大侠指出!不胜感激! 基本数据类型: 大体和C语言类似: boolean truth = true;//逻辑型 //文字型 char c; String st ...
- 51nod1402(贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 题意:中文题诶- 思路:贪心 对于一个桩点,如果我们只考 ...
- python+unittest 搭建简易的接口测试框架
主要介绍如何使用python+unittest快速搭建一个接口测试的框架 1.安装python unittest 2.新建一个python项目ApiTest 在setUp和setDown里设置一些需 ...
- python之文件路径截取 & endswith()
文件路径截取: >>> import os >>> path = '/etc/singfor/passwd/sunny/test.log' >>> ...
- sessionStorage 、localStorage
localStorage和sessionStorage使用时使用相同的API: localStorage.setItem("key","value");//以“ ...
- [題解](最短路/二分)luogu_P1462通往奧格瑞瑪的道路
看到最大的最小值應該想到二分答案,這樣就解決了最小點權的問題,判血量就很好說,直接比較就行, 一個點是二分點權數組,複製一份然後排序,二分下標,速度較快 這麼簡單的題我竟然寫了這麼長時間 #inclu ...
- Web之localStorage
localStorage: 1.localStorage拓展了cookie的4K限制 2.localStorage会可以将第一次请求的数据直接存储到本地,这个相当于一个5M大小的针对于前端页面的数据库 ...
- 牛客寒假5-J.炫酷数学
链接:https://ac.nowcoder.com/acm/contest/331/J 题意: 小希最近想知道一个东西,就是A+B=A|B(其中|为按位或)的二元组有多少个. 当然,直接做这个式子对 ...
- morphia(1)-基础
二.Mapping classes entity类上加注解:@Entity,其成员变量必须有@Id @Id private ObjectId id; 其在mongodb中变量名: _id @Embed ...