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 ...
随机推荐
- PAT——1063. 计算谱半径
在数学中,矩阵的“谱半径”是指其特征值的模集合的上确界.换言之,对于给定的n个复数空间的特征值{a1+b1i, ..., an+bni},它们的模为实部与虚部的平方和的开方,而“谱半径”就是最大模. ...
- BroadcastReceiver(接收广播)
Broadcast Receiver用于接收并处理广播通知(broadcast announcements).多数的广播是系统发起的,如地域变换.电量不足.来电来信等.程序也能够播放一个广播. 程序能 ...
- windows设置控制台编码格式为UTF-8
1.运行CMD 2.输入CHCP查看当前编码格式. 3.输入 CHCP 65001设置编码方式为UTF-8 注意,到此处并没有完全修改,只是修改了当前页面,怎么完全修改? 4.右击控制台,选择属性: ...
- Python 学习笔记(十四)Python类(一)
基本概念 问题空间:问题空间是问题解决者对一个问题所达到的全部认识状态,它是由问题解决者利用问题所包含的信息和已贮存的信息主动的地构成的. 初始状态:一开始时的不完全的信息或令人不满意的状况: 目标状 ...
- oo第二次总结作业
OO电梯作业总结 这三周的作业和课堂内容以及OS的课上内容都相同,都是关于多线程方面的知识.在这次作业中由浅入深布置了三项多线程电梯方面的作业,让我们在实践中感受了多线程的工作原理以及各项需要注意的要 ...
- ueditor getshell漏洞重现及分析
0x00 概述 8月21日,网上爆出ueditor .net版本getshell漏洞,由于只校验ContentType而没校验文件后缀导致getshell. 0x01 漏洞重现 Payload: &l ...
- 双硬盘双系统win10+manjaro-kde搭建
电脑sdd+hdd双硬盘,默认win10装在了sdd分区,uefi+gpt引导.现在想要在hdd中划分出一个分区安装manjaro,并在开机多重引导. 1. 制作安装盘 先去下载最新的镜像,最好在国内 ...
- WebGl 画线
效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Wavesurfer.js音频播放器插件的使用教程
Wavesurfer.js是一款基于HTML5 canvas和Web Audio的音频播放器插件,本文主要记录它及其视觉效果插件Regions插件的使用方法. 1.创建实例 引入插件 import W ...
- 前端基础-HTTP协议
一. HTTP协议简介 二. HTTP协议之请求Request 三. HTTP协议之响应Response 四. HTTP协议完整工作流程 五. HTTP协议关键性总结 六.自定义套接字分析HTTP协议 ...