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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. [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. ...

  6. 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 ...

  7. LeetCode Number of Islands 岛的数量(DFS,BFS)

    题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. datetime.strptime格式转换报错ValueError

    今天遇到一个报错:ValueError: time data '2018-10-10(Wednesday) AM0:50' does not match format '%Y-%m-%d(%A) %p ...

  2. DBProxy 项目全解

    转载自:https://github.com/Meituan-Dianping/DBProxy/blob/master/doc/USER_GUIDE.md#2 1 总体信息        1.1 关于 ...

  3. vue-12-渲染函数 & JSX

    render() Vue.component('anchored-heading', { render: function (createElement) { return createElement ...

  4. dedecms中调用隐藏栏目的方法

    第一种情况用SQL标签如下: {dede:sql sql='Select * from dme_arctype where ishidden=1 and topid=2'} <span clas ...

  5. 4.7 C++ dynamic_cast操作符

    参考:http://www.weixueyuan.net/view/6377.html 总结: 产生这种运行期的错误原因在于static_cast强制类型转换时并不具有保证类型安全的功能,而C++提供 ...

  6. python学习二三事儿(转,整)

    Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...

  7. java.lang.String 使用介绍

    这里我们将总结字符串相关的知识,除了总结String的API用法,同时我们还会总结一些相关的知识点,包括字符串常量池.StringBuffer.StringBuilder,以及equals和==的用法 ...

  8. OneinStack 安装

    安装步骤 注意 如果有单独数据盘,建议您先挂载数据盘,建议将网站内容.数据库放在数据盘中.如何挂载数据盘,请参考(支持阿里云.腾讯云):<如何利用脚本自动化挂载数据盘?> yum -y i ...

  9. 整理有关浏览器兼容性的css样式

    去掉IE自带的删除功能的×号 input::-ms-clear{display:none;} 去掉IE自带密码框的眼睛样式 input::-ms-reveal{display:none;}

  10. ubuntu 安装 mysql 的正确姿势

    1.下载官方提供的mysql-apt-config.deb包进行APT源设置,下载地址:https://dev.mysql.com/downloads/repo/apt/ 2. // 将 mysql- ...