problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::pacific_atlantic_waterflow::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< vector< int > > pacificAtlantic (vector< vector< int > > &heights)
 

详细描述

在文件 leetcode.h2862 行定义.

成员函数说明

◆ pacificAtlantic()

vector< vector< int > > leetcode::pacific_atlantic_waterflow::Solution::pacificAtlantic ( vector< vector< int > > &  heights)
static

在文件 leetcode.cpp7896 行定义.

7896 {
7897 const int m = heights.size();
7898 const int n = heights[0].size();
7899 unordered_set<pair<int, int>, myhash, myeq> pacific;
7900 unordered_set<pair<int, int>, myhash, myeq> atlantic;
7901 queue<pair<int, int>> q;
7902 vector<vector<int>> ans;
7903
7904 for(int i = 0; i < m; i++) {
7905 q.push({i, 0});
7906 pacific.insert({i, 0});
7907 }
7908 for(int i = 1; i < n; i++) {
7909 q.push({0, i});
7910 pacific.insert({0, i});
7911 }
7912 while(!q.empty()) {
7913 auto [x, y] = q.front();
7914 q.pop();
7915 pair<int, int> nexts[4] = {{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}};
7916 for(auto &[next_x, next_y]: nexts) {
7917 if(next_x >= 0 && next_y >= 0 && next_x < m && next_y < n && heights[next_x][next_y] >= heights[x][y] && !pacific.contains({next_x, next_y})) {
7918 pacific.insert({next_x, next_y});
7919 q.push({next_x, next_y});
7920 }
7921 }
7922 }
7923
7924 for(int i = 0; i < m; i++) {
7925 q.push({i, n - 1});
7926 atlantic.insert({i, n - 1});
7927 }
7928 for(int i = 0; i < n - 1; i++) {
7929 q.push({m - 1, i});
7930 atlantic.insert({m - 1, i});
7931 }
7932 while(!q.empty()) {
7933 auto [x, y] = q.front();
7934 q.pop();
7935 pair<int, int> nexts[4] = {{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}};
7936 for(auto &[next_x, next_y]: nexts) {
7937 if(next_x >= 0 && next_y >= 0 && next_x < m && next_y < n && heights[next_x][next_y] >= heights[x][y] && !atlantic.contains({next_x, next_y})) {
7938 atlantic.insert({next_x, next_y});
7939 q.push({next_x, next_y});
7940 }
7941 }
7942 }
7943
7944 for(const auto &p: atlantic) {
7945 if(pacific.contains(p)) {
7946 vector vec = {p.first, p.second};
7947 ans.emplace_back(vec);
7948 }
7949 }
7950 sort(ans.begin(), ans.end());
7951 return ans;
7952 }
int vec[100010]
Definition: pat.cpp:5095

引用了 pat::a::a7_2::vec.

被这些函数引用 leetcode::pacific_atlantic_waterflow::TEST().


该类的文档由以下文件生成: