前言
hello 大家好啊,今天是第七天啦,加油加油。
🐱🐱🐱
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int rows = grid.size(), cols = grid[0].size();
int ans = 0;
for(int i = rows-1; i >= 0; --i)
{
for(int j = cols-1; j >= 0; --j)
{
if(grid[i][j] < 0)
++ans;
}
}
return ans;
}
};
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int n = mat.size();
int sum = 0;
for(int i = 0; i < n; ++i)
{
sum += mat[i][i];
}
for(int i = 0; i < n; ++i)
{
if(n-i-1 != i)
sum += mat[i][n-i-1];
}
return sum;
}
};
单独统计每一行的再比较就行
class Solution {
public:
int maximumWealth(vector<vector<int>>& accounts) {
int maxSum = 0;
for(int i = 0;i < accounts.size(); ++i)
{
int sum = 0;
for(int j = 0; j < accounts[i].size(); ++j)
{
sum += accounts[i][j];
}
maxSum = max(maxSum, sum);
}
return maxSum;
}
};
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
if(rows == 1 || cols == 1)
return true;
for(int i = 0; i < rows-1; ++i)
{
for(int j = 0; j < cols-1; ++j)
{
if(matrix[i][j] != matrix[i+1][j+1])
return false;
}
}
return true;
}
};
class Solution {
public:
vector<int> luckyNumbers (vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
vector<int> row(rows, 0);
vector<int> col(cols, 0);
vector<int> ret;
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++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 < rows; ++i)
{
if(i == col[row[i]])
{
ret.push_back(matrix[i][row[i]]);
}
}
return ret;
}
};
class Solution {
public:
int numSpecial(vector<vector<int>>& mat) {
int rows = mat.size();
int cols = mat[0].size();
int rowCnt[rows];
int colCnt[cols];
memset(rowCnt, 0, sizeof(rowCnt));
memset(colCnt, 0, sizeof(colCnt));
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
int curr = mat[i][j] == 1;
rowCnt[i] += curr;
colCnt[j] += curr;
}
}
int res = 0;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
if (mat[i][j] == 1 && rowCnt[i] == 1 && colCnt[j] == 1)
{
++res;
}
}
}
return res;
}
};
class Solution {
constexpr static int dx[4] = {0, 1, 0, -1};
constexpr static int dy[4] = {1, 0, -1, 0};
public:
int islandPerimeter(vector<vector<int>> &grid) {
int n = grid.size(), m = grid[0].size();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j]) {
int cnt = 0;
for (int k = 0; k < 4; ++k) {
int tx = i + dx[k];
int ty = j + dy[k];
if (tx < 0 || tx >= n || ty < 0 || ty >= m || !grid[tx][ty]) {
cnt += 1;
}
}
ans += cnt;
}
}
}
return ans;
}
};
尾声
🌹🌹🌹
写文不易,如果有帮助烦请点个赞~ 👍👍👍
Thanks?(・ω・)ノ🌹🌹🌹
😘😘😘
👀👀由于笔者水平有限,在今后的博文中难免会出现错误之处,本人非常希望您如果发现错误,恳请留言批评斧正,希望和大家一起学习,一起进步ヽ( ̄ω ̄( ̄ω ̄〃)ゝ,期待您的留言评论。 附GitHub仓库链接
|