LC 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 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
参考答案
class Solution {
public:
void foo(vector<vector <char>> &grid,int i, int j){
if(i< || j< || i>=grid.size() || j>=grid[].size()) return ;
if(grid[i][j] == '' ) return; //为了递归准备的停止条件
grid[i][j] = ''; // 为了避免重复,直接将检测过的value归零
foo(grid,i-,j);
foo(grid,i+,j);
foo(grid,i,j-);
foo(grid,i,j+);
}
int numIslands(vector<vector<char>>& grid) {
int counter = ;
for(int i = ; i<grid.size();i++){
for(int j = ; j<grid[i].size();j++){
if(grid[i][j] == ''){
counter ++;
foo(grid,i,j);
}
}
}
return counter;
}
};
额外补充
SRGA
这道题目让我想到了 种子区域生长算法(Seeded Regin Growing Algorithm),参考了一下别人的答案,还真的十分类似。
基本思路是:种下一个种子(遇到的第一个1区域),然后让它生长(递归)。如果是相同区域,就生长(7~10 行),如果不同(4~5行),就停止。
因为区域种子生长是为了,给所有邻接的区域标记label,从而更好识别图象。但这里不需要label,只需要counter。
防止重复
由初始点位,开始生长,由于递归规则相同,所以初始点位也会进入递归,为了防止重复,只要被接触的区域,全部重新赋值为0。(6行)
的确是一个很聪明的做法。
LC 200 Number of Islands的更多相关文章
- 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 用了第一种方式, ...
- 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 s ...
- 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 ...
- (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 ...
- 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(其实就是一个深搜)
题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...
- [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 ...
随机推荐
- Python基础之range()
range:指定范围,生成指定数字. 1. range() for i in range(1, 10): print(i) 执行结果为: 1 2 3 4 5 6 7 8 9 2. range()步长 ...
- 【洛谷4001】 [ICPC-Beijing 2006]狼抓兔子(最小割)
传送门 洛谷 Solution 直接跑最小割板子就好了. 代码实现 #include<stdio.h> #include<stdlib.h> #include<strin ...
- iOS (APP)进程间8中常用通信方式总结
1 URL Scheme 2 Keychain 3 UIPasteboard 4 UIDocumentInteractionController 5 local socket 6 AirDrop 7 ...
- 对 Python 迭代的深入研究
在程序设计中,通常会有 loop.iterate.traversal 和 recursion 等概念,他们各自的含义如下: 循环(loop),指的是在满足条件的情况下,重复执行同一段代码.比如 Pyt ...
- Android解决AVD Hardware Buttons 和DPAD无法使用问题
如图所示按键用鼠标点击时无法响应. 解决方案: 以我创建的AVD名为Tablet为例 1.找到用户目录(我的用户目录yummy),然后进入如下目录 mac: ~/yummy/.android/avd/ ...
- UDDI:百科
ylbtech-UDDI:百科 UDDI是一种用于描述.发现.集成Web Service的技术,它是Web Service协议栈的一个重要部分.通过UDDI,企业可以根据自己的需要动态查找并使用Web ...
- 约束布局ConstraintLayout详解
约束布局ConstraintLayout详解 转 https://www.jianshu.com/p/17ec9bd6ca8a 目录 1.介绍 2.为什么要用ConstraintLayout 3.如何 ...
- .SpringIOC容器
创建对象 SpringIOC容器,是spring核心内容. 作用: 创建对象 & 处理对象的依赖关系 IOC容器创建对象: 创建对象, 有几种方式: 1) 调用无参数构造器 2) 带参数构造器 ...
- C# 打包安装部署 属性中找不到 查找目标或打开文件位置
用第三方工具OrcaMis (一个可以修改msi文件的工具)来实现的 最后我又试了几次,以为是再程序打包的时候设置有问题,结果都没有找到原因,没有办法只有需求网络资源,网络上有朋友说VS创建的快捷方式 ...
- 001-软件架构概览、maven补充【分包工程、合并包、web容器插件】、git补充
一.整体概述 1.1.共性问题 技术瓶颈.不成体系.不能实际使用.不能落地.无法入门 1.2.目标-软件架构 专注于构建:高可扩展.高性能.大数据量.高并发.分布式的系统架构. 各项技术.组合构建分布 ...