Description

Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.

Find the number of islands.

Example

Given graph:

[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]

return 3.

解题:算岛屿的个数。因为连接在一起的陆地都算是岛屿,所以当确定有一一个格子是陆地时,它周围的格子即使是陆地,也不能重复计算,而应该当成海洋(对应于下面代码中的dfs函数)。代码如下:

public class Solution {
/**
* @param grid: a boolean 2D matrix
* @return: an integer
*/
public int numIslands(boolean[][] grid) {
// write your code here
int res = 0;
int m = grid.length;
if(m == 0)
return 0;
int n = grid[0].length;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == true){
res++;
dfs(grid, i, j);
}
}
}
return res;
}
public void dfs(boolean[][]grid, int i, int j){
if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length){
return;
}
if(grid[i][j] == true){
grid[i][j] = false;
dfs(grid, i-1, j);
dfs(grid, i+1, j);
dfs(grid, i, j-1);
dfs(grid, i, j+1);
}
}
}

433. Number of Islands【LintCode java】的更多相关文章

  1. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  2. 407. Plus One【LintCode java】

    Description Given a non-negative number represented as an array of digits, plus one to the number. T ...

  3. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  4. 373. Partition Array by Odd and Even【LintCode java】

    Description Partition an integers array into odd number first and even number second. Example Given  ...

  5. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  6. 254. Drop Eggs【LintCode java】

    Description There is a building of n floors. If an egg drops from the k th floor or above, it will b ...

  7. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  8. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  9. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

随机推荐

  1. TabActivity 、fragemnt+fragment 和 Fragment+Viewpager 优缺点

    1 TabActivity : 1 过时了 . 2 activity . 是作为android的四大组件...                   重量级的家伙   ViewGroup   : 特别麻 ...

  2. ArrayList两个对象之间的赋值

    List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList&l ...

  3. iOS 数据安全、数据加密传输

    近期接到一个新需求:APP企业版需要接入热更新功能. 热更新需要下发补丁脚本, 脚本下发过程中需要保证脚本传输安全,且需要避免中间人攻击. 需要用到数据加密传输方面的知识,以下是我设计的加密解密流程: ...

  4. 由使用request-promise-native想到的异步处理方法

    由使用request-promise-native想到的异步处理方法 问题场景 因为js语言的特性,使用node开发程序的时候经常会遇到异步处理的问题.对于之前专长App开发的我来说,会纠结node中 ...

  5. windows 使用npm安装webpack 4.0以及配置问题的解决办法

    输入cmd点击打开 输入node -v 出现nodejs版本号 输入npm -v 出现npm版本号则安装npm安装成功, 2.安装webpack 桌面新建一个webpack-test文件夹,点击进入文 ...

  6. 利用谷歌插件破解今日头条的新闻ajax参数加密,新手都能懂

    最近在学习谷歌插件,想找个项目练练手,就拿今日头条开刀 首先访问地址是:https://www.toutiao.com/c/user/50025817786/#mid=50044041847 通过抓包 ...

  7. Leecode刷题之旅-C语言/python-112 路径总和

    /* * @lc app=leetcode.cn id=112 lang=c * * [112] 路径总和 * * https://leetcode-cn.com/problems/path-sum/ ...

  8. Selenium_python自动化环境搭建篇

    説 明: 本篇随笔讲解Selenium+python自动化环境的搭建,此随笔暂不介绍Selenium3,Selenium3需要考虑环境依赖驱动等相关问提比较多一篇随笔没法説完,所以暂不介绍,当然你可以 ...

  9. Simulating Mouse Events in JavaScript

    http://marcgrabanski.com/simulating-mouse-click-events-in-javascript/

  10. 20155226 2016-2017-2 《Java程序设计》课程总结

    20155226 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:师生关系 预备作业2:优秀技能经验 预备作业3:虚拟机linux初接触 第一周学习总结: ...