文章目录
今日知识总结
-
矩阵:定义
int a[3][4]; int a[3][4] = { {5, 0, 2, 4}, {1, 3, 2, 7}, {4, 0, 0, 9}, } int a[ ][4] = { {5, 0, 2, 4}, {1, 3, 2, 7}, {4, 0, 0, 9}, } int a[100][4] = { {5, 0, 2, 4}, {1, 3, 2, 7}, {4, 0, 0, 9}, }
-
矩阵的水平翻转
-
矩阵的垂直翻转
-
矩阵的顺时针翻转
-
矩阵的逆时针翻转
-
矩阵的转置
-
二维数组的函数传参
int diagonalSum(int ** mat, int matSize, int* matCatch )
题目分析
-
-
题目链接:https://leetcode-cn.com/problems/count-negative-numbers-in-a-sorted-matrix/
-
思路:遍历列表,找比零小的数
-
代码:
class Solution { public: int countNegatives(vector<vector<int>>& grid) { int cnt = 0; int xlen = grid.size(), ylen = grid[0].size(); for(int i = 0; i < xlen; i++){ for(int j = 0; j < ylen; j++){ if(grid[i][j] < 0) cnt++; } } return cnt; } };
-
-
-
题目链接:https://leetcode-cn.com/problems/matrix-diagonal-sum/
-
思路:一个矩形有两条对角线,第一条从第一行第一列开始,第二条从第一行最后一列开始,我们可以在同一时间分别找到在两条对角线上的值直接相加就可以了
-
代码:
class Solution { public: int diagonalSum(vector<vector<int>>& mat) { int x = 0, y = 0; int p = 0; int q = mat[0].size() - 1; int sum = 0; while(1){ if(x == mat.size() && y == mat.size() ) break; sum += mat[x][y]; sum += mat[p][q]; if( y == q) sum -= mat[p][q]; x++; y++; p++; q--; } return sum; } };
-
-
-
题目链接:https://leetcode-cn.com/problems/richest-customer-wealth/
-
思路:根据题目模拟
-
代码:
class Solution { public: int maximumWealth(vector<vector<int>>& accounts) { int max = 0; for(int i = 0; i < accounts.size() ; i++){ int sum = 0; for(int j = 0; j < accounts[0].size(); j++){ sum += accounts[i][j]; } if(sum > max) max = sum; } return max; } };
-
-
-
题目链接:https://leetcode-cn.com/problems/toeplitz-matrix/
-
思路:遍历即可
-
代码:
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; } };
-
今日收获:
- 二维数组的传参方式是我第一次接触到的感觉比较新奇
今日疑问:
- 我认为矩阵的反转定义很好理解,但是如何实现还是要好好想想的