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

Solution: dfs or bfs or union find

class Solution {
public int numIslands(char[][] grid) {
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'){
res++;
dfs(grid, i, j);
}
}
}
return res;
}
void dfs(char[][] grid, int i, int j){
if(i >= grid.length || i<0 || j>=grid[0].length || j < 0 || grid[i][j] == '0') return;
grid[i][j] = '0'; // visited
dfs(grid, i-1, j);
dfs(grid, i+1, j);
dfs(grid, i, j-1);
dfs(grid, i, j+1);
}
}

FOllow up get max number of islands

695

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
Example 2:
[[0,0,0,0,0,0,0,0]]
Given the above grid, return 0.
Note: The length of each dimension in the given grid does not exceed 50.

SOlution dfs + array to store the number

class Solution {
int[] a;
public int maxAreaOfIsland(int[][] grid) {
a = new int[grid.length*grid[0].length];
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){
dfs(grid, i, j, res);
res++;
}
}
}
int max = 0;
for(int i = 0; i<res; i++){
//System.out.println(a[i]);
if(max < a[i]) max = a[i];
}
return max;
}
void dfs(int[][] grid, int i, int j,int res){
if(i >= grid.length || i<0 || j>=grid[0].length || j < 0 || grid[i][j] == 0) return;
a[res]++;
grid[i][j] = 0; // visited
dfs(grid, i-1, j, res);
dfs(grid, i+1, j, res);
dfs(grid, i, j-1, res);
dfs(grid, i, j+1, res);
}
}

There is another aolution with dfs without array ....

class Solution {
int[][] grid;
boolean[][] seen; public int area(int r, int c) {
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length ||
seen[r][c] || grid[r][c] == 0)
return 0;
seen[r][c] = true;
return (1 + area(r+1, c) + area(r-1, c)
+ area(r, c-1) + area(r, c+1));
} public int maxAreaOfIsland(int[][] grid) {
this.grid = grid;
seen = new boolean[grid.length][grid[0].length];
int ans = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
ans = Math.max(ans, area(r, c));
}
}
return ans;
}
}

200. Number of Islands + 695. Max Area of Island的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. [leetcode]python 695. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  3. LeetCode 695. Max Area of Island (岛的最大区域)

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. 【easy】695. Max Area of Island

    题目: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) ...

  5. 695. Max Area of Island最大岛屿面积

    [抄题]: 求最多的联通的1的数量 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (repre ...

  6. 695. Max Area of Island@python

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  7. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  8. [Leetcode]695. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. 695. Max Area of Island

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

随机推荐

  1. Nodejs 实现windows后台运行

    首先需要到http://nssm.cc/download/?page=download 下载 nssm 下下来之后是压缩包形式的,解压之后 ctrl + R 进入cmd 命令行界面 在命令行模式下进入 ...

  2. 4、在Shell程序中的使用变量

    学习目标变量的赋值变量的访问变量的输入 12-4-1 变量的赋值在Shell编程中,所有的变量名都由字符串组成,并且不需要对变量进行声明.要赋值给一个变量,其格式如下:变量名=值.注意:等号(=)前后 ...

  3. 设计模式之第7章-外观模式(Java实现)

    设计模式之第7章-外观模式(Java实现) “鱼哥,知道怎么把大象装进冰箱里面么?”(作者按:这么简单的问题还想考我,早了几百年吧.)“把大象装进冰箱里,一共需要三步:第一步,把冰箱门打开:第二步,把 ...

  4. .net IoC 之 Spring.Net 适合刚开始使用

    Spring.Net包括控制反转(IoC) 和面向切面(AOP),这篇文章主要说下IoC方面的入门. 一.首先建立一个MVC项目名称叫SpringDemo,然后用NuGet下载spring(我用的是S ...

  5. java--集合框架总结1--set总结

    一.集合框架的概述. 基础的数据结构有数组,链表,栈,队列,二叉树等,java中的数据结构,利用了这些基本的数据结构分别实现了很丰富的集合框架类型,下面简单地总结下关于java集合框架的基础内容,在进 ...

  6. 五、mybatis集成使用

    1.添加依赖 <!-- mybatis-spring集成--> <dependency> <groupId>org.mybatis.spring.boot</ ...

  7. 卸载或安装程序出现:The feature you are trying to use is on a network resource ...

    卸载或安装程序出现:The feature you are trying to use is on a network resource ... 这种情况可能是因为原先已经安装过这个软件,所以要先卸载 ...

  8. maven仓库中的LastUpdated文件删除脚本

    cleanLastUpdated.bat(windows版本) @echo off rem create by NettQun rem 这里写你的仓库路径 set REPOSITORY_PATH=D: ...

  9. 第3章 css属性color的RGBA值

    颜色之RGBA RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法: color:rgba ...

  10. BZOJ2882: 工艺(后缀数组)

    题意 题目链接 Sol 直接把序列复制一遍 后缀数组即可 在前\(N\)个位置中取\(rak\)最小的输出 #include<bits/stdc++.h> using namespace ...