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. Visual Studio中判断项目的类型

    1. 打开项目的属性,查看Application的Output type

  2. 大型网站技术学习-3. 容器Docker与kubernetes

    大型网站技术基石篇-容器Docker与kubernetes   Docker和Kubernetes的关系就如Xen与OpenStack. Docker是一种容器技术,和Hypervisor(KVM/X ...

  3. WPF Window对象的生命周期

    WPF中所有窗口的基类型都是System.Windows.Window.Window通常用于SDI(SingleDocumentInterface).MDI(MultipleDocumentInter ...

  4. Linux修改命令行样式

    Linux修改Shell命令提示符及颜色 Linux修改Shell命令提示符及颜色 1. Linux登录过程中加载配置文件顺序: /etc/profile → /etc/profile.d/*.sh ...

  5. 【转】WCF 服务第一次调用慢的问题

    写了一个WCF Serivces供外部程序通过.NET Businesss Connector调用AX的代码,第一次调用的时候总是很慢,有时候甚至超过1分钟,访问地址改成http://localhos ...

  6. Ajax知识点复习

    1. ajax是什么? * asynchronous javascript and xml:异步的js和xml * 它能使用js访问服务器,而且是异步访问! * 服务器给客户端的响应一般是整个页面,一 ...

  7. C#把大写英文变成小写英文,把小写英文变成大写英文

    static void Main(string[] args) { string s;  // 声明一个变量,来接受用户输入的值. Console.WriteLine("请输入一个字符串:& ...

  8. SSH框架整合中Hibernate实现Dao层常用结构

    一.疑惑 一直以来,我在使用SSH框架的时候经常会发现后者有疑虑到底使用hibernate的那种方法或者如何配置hibernate来操作数据库,经过 一段时间的学习下面我来总结一下,常用的dao层配置 ...

  9. Eclipse使用快捷键总结

    1.为方法添加注释:Alt + Shift + J

  10. html和css入门 (四)

    背景样式 背景颜色 属性名 background-color 属性值 合法的颜色的名,比如:red:十六进制值,比如:#ff0000:RGB 值,比如:rgb(255,0,0) 默认值 transpa ...