Leetcode 200 Number of Islands DFS
统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS
class Solution {
public:
int m, n;
bool is_in(int x, int y)
{
return (x < m ) && (x >= ) && (y < n ) && (y >= );
} void dfs(std::vector<std::vector<char>> &board, int x, int y)
{
board[x][y] = '';
int dir[][] = { { , }, { -, }, { , }, { , - } };
for (int i = ; i < ; ++i){
int tx = x + dir[i][];
int ty = y + dir[i][];
if (is_in(tx, ty) && board[tx][ty] == '')
{
dfs(board, tx, ty);
}
}
} int numIslands(std::vector<std::vector<char>>& grid)
{
m = grid.size();
if (m == ) return ;
n = grid[].size();
if (n == ) return ;
int ans = ;
for (int i = ; i < m; ++i){
for (int j = ; j < n; ++j){
if (grid[i][j] == ''){
dfs(grid, i, j);
ans++;
}
}
}
return ans;
}
};
Leetcode 200 Number of Islands DFS的更多相关文章
- 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 用了第一种方式, ...
- [LeetCode] 200. Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- (BFS/DFS) leetcode 200. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Leetcode 200. number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] 200. Number of Islands 解题思路
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 200. Number of Islands(DFS)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode 200. Number of Islands 岛屿数量(C++/Java)
题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...
随机推荐
- 缓存大全(Memcached、redis、RabbitMQ )
Memcached: 简介.安装.使用 python操作Memcached Memcached天生支持集群 Redis: 简介.安装.使用.实例 Python操作Redis String.Hash.L ...
- Angularjs学习---angularjs环境搭建,ubuntu 12.04下安装nodejs、npm和karma
1.下载angularjs 进入其官网下载:https://angularjs.org/,建议下载最新版的:https://ajax.googleapis.com/ajax/libs/angular ...
- POJ 2175 Evacuation Plan 费用流 负圈定理
题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在 ...
- mysql 4种启动方式
mysql 4种启动方式 都是去调用mysqld文件 1. mysqld 启动 进入mysqld文件所在目录(/../libexec/mysqld) ./mysqld --defaults-file= ...
- Google搜索质量评估员指南
Google: 此文档是我们(谷歌)的一份搜索质量评估员指南,可作为搜索质量评估员的培训材料.其中主要介绍了一类名为“网址评分”的评分任务,此类任务要求评估员查看搜索查询与可能返回的相应结果.他们需要 ...
- struts2 表单验证
http://www.blogjava.net/javagrass/archive/2011/11/21/364400.html
- Hadoop中HDFS的管理
本文讲述怎么在Linux Shell中对HDFS进行操作. 三种命令格式: hadoop fs适用于任何不同的文件系统,比如本地文件系统和HDFS文件系统 hadoop dfs只能适用于HDFS文件系 ...
- return和exit函数的区别
在上Linux课的时候,老师提到一句,调用vfork产生的子进程就是为了使用exec族函数来执行其他的代码逻辑. 在子进程退出的时候有两种方式,exit和exec族函数,不能使用return,为什么不 ...
- C# 基础(3)
跳转语句 Goto(现在一般不使用) 标志位 Flag true false 常量:(不可改变的量) 语法: Const 类型 变量名 = 常量值 在定义时赋值,其他地方不能赋值 枚举 是自己定义了一 ...
- php执行流程
1.源码文件编写,php源文件 2.词法分析,token 3.语法分析,生成opcode并组成opcode array 4.执行opcode,返回结果