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

#include <leetcode.h>

静态 Public 成员函数

static int numEnclaves (vector< vector< int > > &grid)
 

详细描述

在文件 leetcode.h1066 行定义.

成员函数说明

◆ numEnclaves()

int leetcode::number_of_enclaves::Solution::numEnclaves ( vector< vector< int > > &  grid)
static

在文件 leetcode.cpp2676 行定义.

2676 {
2677 int sum = 0;
2678 const int m = grid.size();
2679 const int n = grid[0].size();
2680 auto que = queue<pair<int, int>>();
2681 for(int i = 0; i < m; i++) {
2682 for(int j = 0; j < n; j++) {
2683 if(grid[i][j] == 1) {
2684 sum++;
2685 if(i == 0 || i == m - 1 || j == 0 || j == n - 1) {
2686 que.push(make_pair(i, j));
2687 grid[i][j] = 0;
2688 }
2689 }
2690 }
2691 }
2692 while(!que.empty()) {
2693 auto [x, y] = que.front();
2694 sum--;
2695 que.pop();
2696 pair<int, int> nexts[4] = {make_pair(x + 1, y), make_pair(x - 1, y), make_pair(x, y + 1), make_pair(x, y - 1)};
2697 for(auto next: nexts) {
2698 auto [next_x, next_y] = next;
2699 if(0 <= next_x && next_x < m && 0 <= next_y && next_y < n && grid[next_x][next_y] != 0) {
2700 que.push(next);
2701 grid[next_x][next_y] = 0;
2702 }
2703 }
2704 }
2705 return sum;
2706 }

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


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