第七天《二维数组》

文章目录

今日知识总结

  1. 矩阵:定义

    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},
    }
    
  2. 矩阵的水平翻转

  3. 矩阵的垂直翻转

  4. 矩阵的顺时针翻转

  5. 矩阵的逆时针翻转

  6. 矩阵的转置

  7. 二维数组的函数传参

    int diagonalSum(int ** mat, int matSize, int* matCatch )
    

题目分析

  1. 1351. 统计有序矩阵中的负数

    1. 题目链接:https://leetcode-cn.com/problems/count-negative-numbers-in-a-sorted-matrix/

    2. 思路:遍历列表,找比零小的数

    3. 代码:

      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;
      
          }
      };
      
  2. 1572. 矩阵对角线元素的和

    1. 题目链接:https://leetcode-cn.com/problems/matrix-diagonal-sum/

    2. 思路:一个矩形有两条对角线,第一条从第一行第一列开始,第二条从第一行最后一列开始,我们可以在同一时间分别找到在两条对角线上的值直接相加就可以了

    3. 代码:

      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;
      
          }
      };
      
  3. 1672. 最富有客户的资产总量

    1. 题目链接:https://leetcode-cn.com/problems/richest-customer-wealth/

    2. 思路:根据题目模拟

    3. 代码:

      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;
      
          }
      };
      
  4. 766. 托普利茨矩阵

    1. 题目链接:https://leetcode-cn.com/problems/toeplitz-matrix/

    2. 思路:遍历即可

    3. 代码:

      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;
          }
      };
      
      

今日收获:

  1. 二维数组的传参方式是我第一次接触到的感觉比较新奇

今日疑问:

  1. 我认为矩阵的反转定义很好理解,但是如何实现还是要好好想想的

资料链接

博客链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值