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. shutil 模块

    import shutil #用于简化文件操作的模块 # f1 = open(r"D:\上海python全栈4期\day20\7.shutil模块.py","rb&quo ...

  2. daay04流程控制之for循环

    for循环主要用于循环取值 student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb'] # i=0 # while i < len(student ...

  3. C++输出数组名

    1.如果C++输出的数组是char类型的,那么输出的就是数组中的元素. 2.如果使用的是其他类型的数组作为输出的话,那么就是一个16进制的地址. 3.还是那句话,对数组的操作,很多时候都是指针的操作, ...

  4. hdu3861 强连通分量缩点+二分图最最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. Highcharts 柱图 每个柱子外围的白色边框

    Highcharts 柱图中每条柱外会有默认白色的边框, 去边框代码如下: plotOptions: { bar: { borderColor: "",//去边框 } }

  6. 搭建Django项目

    命令行搭建Django项目 1.安装django 在指定解释器环境下安装django 1.11.9 在真实python3环境下: pip3 install django==1.11.9 在虚拟环境下: ...

  7. ubatu 安装nodejs npm liveserver

    更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties software-properti ...

  8. CentOS Minimal Install

    ifup eth0chkconfig iptables offsestatus vi /etc/sysconfig/network-scripts/ifcfg-eth0 这样设置扣,记得重启网卡:[r ...

  9. 莫烦tensorflow(1)-训练线性函数模型

    import tensorflow as tfimport numpy as np #create datax_data = np.random.rand(100).astype(np.float32 ...

  10. 行为参数化和Lambda表达式

    行为参数化是指拿出一个代码块把他准备好却不执行它.这个代码块以后可以被程序的其他部分调用,意味着你可以推迟这块代码的执行.方法接受多种行为作为参数,并在内部使用来完成不同的行为.行为参数话的好处在于可 ...