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. 转 WINXP VBOX 给UBUNTU 加共享目录方法

    1. 安装增强功能包(Guest Additions)安装好Ubuntu 9.04后,运行Ubuntu并登录.然后在VirtualBox的菜单里选择"设备(Devices)" -& ...

  2. xamarin for android 环境配置

    先安装vs2010,参考以下教程可以进行破解 http://hi.baidu.com/hegel_su/item/2b0771c6aaa439e496445252?qq-pf-to=pcqq.grou ...

  3. solr不是标准的java project解决方案

    官方默认提供的源码包并不是一个标准的Eclipse Java - Project,需要使用ivy进行构建,通过ivy的构建可以将下载下来的源码包转换成一个标准的java Project,然后我们就能把 ...

  4. 在Android下通过ExifInterface类操作图片的Exif信息

    什么是Exif 先来了解什么是Exif.Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了 数码照片的信息,包括拍摄的光圈.快门.平衡白.I ...

  5. 流畅的python和cookbook学习笔记(一)

    1.数据结构 1.1 内置序列类型 四种序列类型: 1.容器序列:list.tuple和collections.deque 2.扁平序列:str.bytes.bytearray.memoryview和 ...

  6. 前端(一):html标签

    HTML(Hypertext Markup Language)超文本标记语言,它负责页面的结构.超文本指的是超链接,使用超链接可以从一个页面跳转到另一个页面. HTML的发展:1993年6月发布第一个 ...

  7. javaweb之jsp标签

    1.JSP标签简介 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 2.JSP常用标签 ...

  8. python文件修改 核心5步,函数实现修改任意文件内容

    文件修改 核心5步1.以读的模式打开原文件,产生句柄f12.以写的模式打开一个新文件,产生句柄f23.读取原文件的内容并将原文件需要替换的内容修改写入到新文件4.删除原文件5.把新文件重名了成原文件 ...

  9. Java Jsp使用

    1.Jsp基础 1)Jsp的执行过程 tomcat服务器完成:jsp文件->翻译成java文件->编译成class字节码文件-> 构造类对象-> 调用方法 tomcat的wor ...

  10. 机智的Popup,带着简单的图表感觉酷酷的

    之前有提过用 InfoTemplate 类对 FeatureLayer 的基本信息进行展示,今天为大家介绍 esri/dijit/Popup 的使用,这东西还有 简单的图表展示功能呢! <!DO ...