433. Number of Islands【LintCode java】
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】的更多相关文章
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 407. Plus One【LintCode java】
Description Given a non-negative number represented as an array of digits, plus one to the number. T ...
- 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 ...
- 373. Partition Array by Odd and Even【LintCode java】
Description Partition an integers array into odd number first and even number second. Example Given ...
- 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 ...
- 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 ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
随机推荐
- Android小游戏应用---撕破美女衣服游戏
ImageView after; ImageView before; @Override protected void onCreate(Bundle savedInstanceState) { su ...
- eclipse删除的文件如何恢复。
1.如果不小心删除了单个文件或者文件夹. 鼠标点击上级目录 Ctrl+z就可以恢复了. 如果没有恢复,就右击项目,选择Resore from Local History 然后选择你所删除的文件,选择恢 ...
- 【腾讯敏捷转型No.7】QQ邮箱如何通过敏捷成为行业第一
前几篇文章讲到2006年的腾讯是如何开始敏捷转型的,接下来这篇文章,我将向大家讲述,腾讯开始敏捷转型之后,QQ邮箱是如何通过敏捷成为行业第一. 众所周知,张小龙是“微信之父”,对他熟悉的人,应该也知道 ...
- Oracle 体系结构五
确定实例是否是RAC数据库的一部分:select parallel from v$instance; 确定数据库是否通过Data Guard备用数据库的保护来防止数据丢失:select protect ...
- Web | jQuery快速上手
jQuery伴随前端走过一段辉煌的时光,虽然现在已经慢慢的走下顶峰,但是过去的很多项目都是用jQuery写的,它的一些封装思想也非常值得借鉴,懂得jQuery是前端必不可少的. jQuery顶级对象 ...
- Scanner(基本用法初学)
package day01; import java.util.Scanner; public class Case05 { public static void main(String[] args ...
- Vue 父组件调用子组件的方法
qwq 前两天看了下vue,父子组件方法的调用,怕忘记,所以做个小记录. 一.父组件调用子组件的方法 1.父组件 <template> <div id="rightmen ...
- 宏观看restframework序列化
序列化 序列化意义 web有两种应用模式,一种是前后端不分离,一种是前后端分离,当前后端分离的时候,后端只需要向前端传输数据即可,不需要进行其他的操作,一般如果是中大型公司,都是前后端分离,这也是目前 ...
- 一个数据仓库时代开始--Hive
一.什么是 Apache Hive? Apache Hive 是一个基于 Hadoop Haused 构建的开源数据仓库系统,我们使用它来查询和分析存储在 Hadoop 文件中的大型数据集.此外,通过 ...
- Hadoop系列-HDFS基础
基本原理 HDFS(Hadoop Distributed File System)是Hadoop的一个基础的分布式文件系统,这个分布式的概念主要体现在两个地方: 数据分块存储在多台主机 数据块采取冗余 ...