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

#include <leetcode.h>

Public 成员函数

int longestIncreasingPath (vector< vector< int > > &matrix)
 

Public 属性

int columns
 
int rows
 

静态 Public 属性

static constexpr int dirs [4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
 

详细描述

在文件 leetcode.h3184 行定义.

成员函数说明

◆ longestIncreasingPath()

int leetcode::longest_increasing_path_in_a_matrix::Solution::longestIncreasingPath ( vector< vector< int > > &  matrix)

在文件 leetcode.cpp9066 行定义.

9066 {
9067 if(matrix.empty() || matrix[0].empty()) {
9068 return 0;
9069 }
9070 rows = matrix.size();
9071 columns = matrix[0].size();
9072 auto outdegrees = vector(rows, vector<int>(columns));
9073 for(int i = 0; i < rows; ++i) {
9074 for(int j = 0; j < columns; ++j) {
9075 for(int k = 0; k < 4; ++k) {
9076 const int newRow = i + dirs[k][0], newColumn = j + dirs[k][1];
9077 if(newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[i][j]) {
9078 ++outdegrees[i][j];
9079 }
9080 }
9081 }
9082 }
9083 queue<pair<int, int>> q;
9084 for(int i = 0; i < rows; ++i) {
9085 for(int j = 0; j < columns; ++j) {
9086 if(outdegrees[i][j] == 0) {
9087 q.push({i, j});
9088 }
9089 }
9090 }
9091 int ans = 0;
9092 while(!q.empty()) {
9093 ++ans;
9094 const int size = q.size();
9095 for(int i = 0; i < size; ++i) {
9096 const auto cell = q.front();
9097 q.pop();
9098 const int row = cell.first, column = cell.second;
9099 for(int k = 0; k < 4; ++k) {
9100 int newRow = row + dirs[k][0], newColumn = column + dirs[k][1];
9101 if(newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] < matrix[row][column]) {
9102 --outdegrees[newRow][newColumn];
9103 if(outdegrees[newRow][newColumn] == 0) {
9104 q.push({newRow, newColumn});
9105 }
9106 }
9107 }
9108 }
9109 }
9110 return ans;
9111 }

引用了 columns, dirs , 以及 rows.

类成员变量说明

◆ columns

int leetcode::longest_increasing_path_in_a_matrix::Solution::columns

在文件 leetcode.h3187 行定义.

被这些函数引用 longestIncreasingPath().

◆ dirs

constexpr int leetcode::longest_increasing_path_in_a_matrix::Solution::dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
staticconstexpr

在文件 leetcode.h3188 行定义.

被这些函数引用 longestIncreasingPath().

◆ rows

int leetcode::longest_increasing_path_in_a_matrix::Solution::rows

在文件 leetcode.h3186 行定义.

被这些函数引用 longestIncreasingPath().


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