文章目录
一、预知识点
二、题目
1、1351. 统计有序矩阵中的负数
题目
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[j][i]>=0) continue;
else {ans+=(m-j);break;}
}
}
return ans;
}
};
2、1572. 矩阵对角线元素的和
题目
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int ans=0,n=mat.size();
for(int i=0;i<n;i++){
ans+=mat[i][i]+mat[i][n-i-1];
}
return n%2==0?ans:ans-mat[n/2][n/2];
}
};
3、1672. 最富有客户的资产总量
题目
在这里插入代码片
4、766. 托普利茨矩阵
题目
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] != matrix[i - 1][j - 1]) {
return false;
}
}
}
return true;
}
};
5、1380. 矩阵中的幸运数
题目
class Solution {
public:
vector<int> luckyNumbers (vector<vector<int>>& matrix) {
vector<int> tes;
int m = matrix.size();
int n = matrix[0].size();
vector<int> row(m,0);
vector<int> col(n,0);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(matrix[i][j]<matrix[i][row[i]]){
row[i]=j;
}
if(matrix[i][j]>matrix[col[j]][j]){
col[j]=i;
}
}
}
for(int i=0;i<m;i++){
if(i==col[row[i]]){
tes.push_back(matrix[i][row[i]]);
}
}
return tes;
}
};
6、1582. 二进制矩阵中的特殊位置
题目
在这里插入代码片
7、463. 岛屿的周长
题目
在这里插入代码片