Leecode 417. 太平洋大西洋水流问题 DFS

news/2024/7/20 22:35:30 标签: 深度优先, leetcode, 算法, c++

原题链接:Leecode 417. 太平洋大西洋水流问题
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
从太平洋和大西洋各自遍历,重合的点即为答案。

class Solution {
public:
    vector<vector<int>> P,A,res;
    void dfs(vector<vector<int>>& heights,vector<vector<int>>& vis,int i,int j)
    {
        int m=heights.size(),n=heights[0].size();
        if(vis[i][j]) return ;
        vis[i][j]=1;
        if(P[i][j] && A[i][j]) res.push_back({i,j});
        if(i && heights[i-1][j]>=heights[i][j]) dfs(heights,vis,i-1,j);
        if(i+1<m && heights[i+1][j]>=heights[i][j]) dfs(heights,vis,i+1,j);
        if(j && heights[i][j-1]>=heights[i][j]) dfs(heights,vis,i,j-1);
        if(j+1<n && heights[i][j+1]>=heights[i][j]) dfs(heights,vis,i,j+1);
    }
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        int m=heights.size(),n=heights[0].size();
        P=A=vector<vector<int>>(m,vector<int>(n));
        for(int j=0;j<n;j++)
        {
            dfs(heights,P,0,j);
            dfs(heights,A,m-1,j);
        }
        for(int i=0;i<m;i++) 
        {
            dfs(heights,P,i,0);
            dfs(heights,A,i,n-1);
        }
        return res;
    }
};

http://www.niftyadmin.cn/n/1264721.html

相关文章

复杂网络中louvain算法实现时报错AttributeError: module ‘community‘ has no attribute ‘best_partition‘

导入包的方式有点奇怪&#xff0c;用的不是包名“python-louvain”而是“community”&#xff0c; import community as community_louvain 在jupyter中运行“partition community_louvain.best_partition(G) #进行图划分”的时候出现以下错误&#xff1a; AttributeError:…

ImportError: cannot import name ‘joblib‘

原因&#xff1a; 安装的Scikit-learn版本太高&#xff0c;我安装的版本是0.23.1 解决方法&#xff1a; 需要将Scikit-learn版本降到0.21以下 pip uninstall joblib scikit-learn sklearn pip install Scikit-learn0.20.4 或者直接安装joblib&#xff1a; pip install j…

Leecode 1732. 找到最高海拔 前缀和

原题链接&#xff1a;Leecode 1732. 找到最高海拔 class Solution { public:int largestAltitude(vector<int>& gain) {int res0,tmp0;for(auto h: gain){tmph;resmax(res,tmp);}return res;} };

Python报错ValueError: arrays must all be same length

在运行Python评分卡模型课程的step2_sklearn_model.py脚本时&#xff0c;报错如下图 第90行代码&#xff0c; df_coefpd.DataFrame({"variable_names":list_vNames,"coef":list_coef}) 报错&#xff1a;ValueError: arrays must all be same length. 原因和…

Leecode 1893. 检查是否区域内所有整数都被覆盖 差分数组

原题链接&#xff1a;Leecode 1893. 检查是否区域内所有整数都被覆盖 class Solution { public:bool isCovered(vector<vector<int>>& ranges, int left, int right) {vector<int> sum(52,0);for(auto node : ranges){int lnode[0],rnode[1];sum[l]1;…

PermissionError: [Errno 13] Permission denied: ‘iv_list.xlsx‘问题解决

ermissionError: [Errno 13] Permission denied: iv_list.xlsx 报错原因&#xff1a;xlsx文件资源被占用&#xff0c;即excel 文件是打开的&#xff0c;写入操作需要关闭文件。 解决办法&#xff1a;关闭excel文件&#xff0c;重新运行。

Leecode 1413. 逐步求和得到正数的最小值 前缀和

原题链接&#xff1a;Leecode 1413. 逐步求和得到正数的最小值 class Solution { public:int minStartValue(vector<int>& nums) {int sum0,resINT_MAX;for(auto num : nums){sumnum;resmin(res,sum);}return res>1 ? 1:1-res;} };

python中dtype、type()、astype()区别

type() 返回数据结构类型&#xff08;list、dict、numpy.ndarray 等&#xff09; dtype() 返回数据元素的数据类型&#xff08;int、float等&#xff09; astype()函数 &#xff11;astype()函数可用于转化dateframe某一列的数据类型 如下将dateframe某列的str类型转为int&…