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. day35 数据库介绍和初识sql

    今日内容: 1. 代码: 简易版socketsever 2.数据库(mysql)简单介绍和分类介绍 3.mysql root修改密码 4.修改字符集编码 5.初识sql语句 1.简易版socketse ...

  2. X86汇编语言实现的贪吃蛇游戏

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  3. Android知识补充(Android学习笔记)

    Android知识补充 ●国际化 所谓的国际化,就是指软件在开发时就应该具备支持多种语言和地区的功能,也就是说开发的软件能同时应对不同国家和地区的用户访问,并针对不同国家和地区的用户,提供相应的.符合 ...

  4. (C/C++学习笔记) 二十四. 知识补充

    二十四. 知识补充 ● 子类调用父类构造函数 ※ 为什么子类要调用父类的构造函数? 因为子类继承父类,会继承到父类中的数据,所以子类在进行对象初始化时,先调用父类的构造函数,这就是子类的实例化过程. ...

  5. node(1) npm是什么?node的异步概念

    NPM是随同的NodeJS一起安装的包管理工具 他可以做什么? 1.可以从NPM服务器下载别人的东西使用 2.可以把自己的东西传到NPM服务器,让别人下载使用 淘宝的镜像会快一点      cnpm ...

  6. c算法:字符串查找-KMP算法

    /* *用KMP算法实现字符串匹配搜索方法 *该程序实现的功能是搜索本目录下的所有文件的内容是否与给定的 *字符串匹配,如果匹配,则输出文件名:包含该字符串的行 *待搜索的目标串搜索指针移动位数 = ...

  7. mysql中sql查询使用注意

    1.注意DESC关键字仅适用于在它前面的列名(birth):不影响species列的排序顺序. SELECT name, species, birth FROM pet ORDER BY specie ...

  8. jmeter源码导入eclipse并执行

    由于JMeter纯Java开发,界面也是基于Swing或AWT搞出来的,所以想更深层次的去了解这款工具或对于想了解JMeter插件开发或二次开发的童鞋们来说,读读JMeter的源码估计是必不可少的,所 ...

  9. IntelliJ IDEA导入Javax包(servlet-api.jar)

    在初次使用 IntelliJ IDEA 中,当你使用javax.servlet包下的类时(例:javax.servlet.http.HttpServlet), 在你会发现在IntelliJ IDEA里 ...

  10. UBUNTU安装 Rabbitvsc可视化版本控制客户端软件

    sudo add-apt-repository ppa:rabbitvcs/ppa sudo apt-get update sudo apt-get install rabbitvcs-core ra ...