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. 51nod算法马拉松B

    首先将原本字符串hash,注意每一个字母要分开了. 然后并查集判断字符相同,将字符ascll吗乘转化为祖先乘. 然后就可以判断相等的情况. 然后考虑相等的情况. 二分枚举中间点,然后如果左边是不相等并 ...

  2. linux-安装jdk以及tomcat

    1.安装jdk   下载地址:www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260.html 将jdk下 ...

  3. Centos常用快捷键

    终端快捷键  tab=补全 ctrl+a=开始位置 ctrl+e=最后位置 ctrl+k=删除此处至末尾所有内容 ctrl+u=删除此处至开始所有内容 ctrl+d=删除当前字母 ctrl+w=删除此 ...

  4. 【重大更新】DevExpress WinForms v18.2新版亮点(七)

    买 DevExpress Universal Subscription  免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...

  5. Final阶段第1周/共1周 Scrum立会报告+燃尽图 06

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2485] 版本控制:https://git.coding.net/liuyy08 ...

  6. Oracle数据库三种备份方案

    Oracle数据库有三种标准的备份方法,它们分别是导出/导入(EXP/IMP).热备份和冷备份.导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一. 导出/导入(Export/Import) 利用 ...

  7. Python 基础day4

    整体大纲关于占位符 tpl = "i am %s" % "alex"   tpl = "i am %s age %d" % ("a ...

  8. 元组类型&字典类型

    一.元组 元组:是一个不可变的列表 用途:当需要记录多个同种属性的值,并且只有读没有改的需求是,这时候应该用到元组 定义方式:在()内用逗号费隔开多个任意类型的元素 ***注意***     当元组只 ...

  9. JS之鼠标改变img

    代码用途: 通过点击图片,来改变图片内容 代码: <!DOCTYPE html> <html> <body> <script> function cha ...

  10. java的异常抛出和String类常用方法

    一.异常抛出 异常是程序的异种非错误的意外情况,分为运行期异常(RuntimeException)和编译期异常(CheckedExcption) 处理异常可以用try——catch或自定义 impor ...