当前位置:网站首页>417.太平洋大西洋水流问题
417.太平洋大西洋水流问题
2022-07-30 05:38:00 【Linke66】
解题思路:
一开始自己的思路是看每一个点是否能流到太平洋和大西洋,很复杂,各种细节考虑不到。看了题解觉得这个方法很好,记录一下。
我们从四个边开始往高处走,能走到的位置就是能流到对应边旁边的洋流。
can_reach_p,(太平洋 pacific)代表能到达太平洋;
can_reach_a,(大西洋 atlantic)代表能到达大西洋。
假如某个点的can_reach_p和can_reach_a都为true,说明符合条件。
具体思路看代码。
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
if(heights.empty()||heights[0].empty()) return {
};
int m=heights.size(),n=heights[0].size();
vector<vector<int>>ret;
vector<vector<bool>> can_reach_p(m,vector<bool>(n,false)); //能到达太平洋 pacific ocean
vector<vector<bool>> can_reach_a=can_reach_p; //能到达大西洋
for(int i=0;i<m;++i){
dfs(heights,can_reach_p,i,0);
dfs(heights,can_reach_a,i,n-1);
}
for(int i=0;i<n;++i){
dfs(heights,can_reach_p,0,i);
dfs(heights,can_reach_a,m-1,i);
}
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(can_reach_a[i][j]&&can_reach_p[i][j]){
ret.push_back({
i,j});
}
}
}
return ret;
}
void dfs(const vector<vector<int>>& heights,
vector<vector<bool>> &can_reach,
int x,int y)
{
if(can_reach[x][y]==true) return;
can_reach[x][y]=true;
if(x-1>=0&&heights[x-1][y]>=heights[x][y])
dfs(heights,can_reach,x-1,y);
if(y-1>=0&&heights[x][y-1]>=heights[x][y])
dfs(heights,can_reach,x,y-1);
if(x+1<heights.size()&&heights[x+1][y]>=heights[x][y])
dfs(heights,can_reach,x+1,y);
if(y+1<heights[0].size()&&heights[x][y+1]>=heights[x][y])
dfs(heights,can_reach,x,y+1);
}
};
边栏推荐
- JVM之GC 调优工具 Arthas 实战使用(二)
- 应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践
- [GStreamer] The name of the plugin should match GST_PLUGIN_DEFINE
- Memories · The last system design in the university era
- 瑞吉外卖项目:新增菜品与菜品分页查询
- It's time to have to learn English, give yourself multiple paths
- navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
- asyncawait和promise的区别
- 排列数字(DAY90)dfs
- pytorch中的线性代数
猜你喜欢
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
postman 请求 post 调用 传 复合 json数据
配环境 / 初步测试
“tensorflow.keras.preprocessing“ has no attribute “image_dataset_from_directory“
mysql高阶语句(一)
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
This dependency was not found:
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
What is SOA (Service Oriented Architecture)?
JVM之GC 调优基础知识(一)
随机推荐
Memories · The last system design in the university era
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
【Pytorch】torch.manual_seed()、torch.cuda.manual_seed() 解释
MySQL stored procedure
idea 编译protobuf 文件的设置使用
字符串(一) 哈希
【线性神经网络】线性回归 / 基础优化方法
分布式事务之 Seata框架的原理和实战使用(三)
839. 模拟堆
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
numpy中np.inf函数的用法讲解
Detailed MySQL-Explain
php数组实现根据某个键值将相同键值合并生成新二维数组的方法
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
安装pytorch
JVM之GC 调优工具 Arthas 实战使用(二)
Thymeleaf简介
[Mysql] DATEDIFF function
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
MySQL(3)