矩阵对角线元素的和 - 简单

*************

c++

topic: 1572. 矩阵对角线元素的和 - 力扣(LeetCode)

*************

Look at the problems immediately.

|----------------------------------------------------------------------------|
| |

vector<vector<int>>& mat means mat is a two-dimension vector. Let's review the basic usage of the creating vector in c++.

make an integer.

cpp 复制代码
int w = 13;
int t = 38;

make a one-dimension vector.

cpp 复制代码
// 直接给定数组,数组的名字是自定义的
vector<int> w = {1, 3, 3, 8};

// 构造一个数组,包含13个元素,每个元素是 38
vector<int> t(13, 38);

make a two-dimension vector. And when talks about two-dimension vector, it is made of many one-dimension vctors.

cpp 复制代码
// 一维数组
vecotr<int> w(13, 38);

输出:
38 38 38 38 38 38 38 38 38 38 38 38 38


// 二维数组就是规定了有几个一维数组、
vector<vector<int>> t(13, vector<int>(13, 38));

输出:
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38
38 38 38 38 38 38 38 38 38 38 38 38 38

I like the basic usages of everything so much. Making full usage of the things keeps claen. Many people want to learn too much skills, which I think donnot have to. Keep things simple.

I think when looking at the mat, getting the size is always first.

cpp 复制代码
class Solution {
public:
    int diagonalSum(vector<vector<int>>& mat) {
        
        int n = mat.size(); // get the size of mat
        int sum = 0;
    }
};

mat[a][b] means the element lies in line a column b.

cpp 复制代码
class Solution {
public:
    int diagonalSum(vector<vector<int>>& mat) {
        
        int n = mat.size(); // get the size of mat
        int sum = 0;

        for (int i = 0; i < n; i++)
        {
            sum = sum + mat[i][i];
            sum = sum + mat[i][n - 1 - i];
        }

        return sum;
    }
};

|----------------------------------------------------------------------------|
| |

This problen is easy but sumething wrong. Soon I find the key point. 5 is really a special one. It lies in both main diagonal and counter diagonal.

|----------------------------------------------------------------------------|
| |

just minus it.

cpp 复制代码
class Solution {
public:
    int diagonalSum(vector<vector<int>>& mat) {
        
        int n = mat.size(); // get the size of mat
        int sum = 0;

        for (int i = 0; i < n; i++)
        {
            sum = sum + mat[i][i];
            sum = sum + mat[i][n - 1 - i];
        }

        // 如果奇数个元素,那么得减掉正中心的元素,因为他被计算了两遍
        if (n % 2 == 1)
        {
            sum = sum - mat[(n - 1) / 2][(n - 1) / 2];
        }

        return sum;
    }
};

|----------------------------------------------------------------------------|
| |

相关推荐
】余1853816280012 小时前
矩阵系统源码搭建与定制化开发,支持OEM
线性代数·矩阵
YuTaoShao2 天前
【LeetCode 热题 100】73. 矩阵置零——(解法一)空间复杂度 O(M + N)
算法·leetcode·矩阵
Dark__Monarch2 天前
二元一次方程
线性代数
Kaltistss2 天前
240.搜索二维矩阵Ⅱ
线性代数·算法·矩阵
说私域2 天前
视频号账号矩阵运营中定制开发开源 AI 智能名片 S2B2C 商城小程序的赋能研究
人工智能·矩阵·开源
张晓~183399481213 天前
数字人源码部署流程分享--- PC+小程序融合方案
javascript·小程序·矩阵·aigc·文心一言·html5
峙峙峙4 天前
线性代数--AI数学基础复习
人工智能·线性代数
我爱C编程4 天前
基于拓扑结构检测的LDPC稀疏校验矩阵高阶环检测算法matlab仿真
算法·matlab·矩阵·ldpc·环检测
CVer儿4 天前
svd分解求旋转平移矩阵
线性代数·算法·矩阵
张晓~183399481214 天前
数字人分身+矩阵系统聚合+碰一碰发视频: 源码搭建-支持OEM
线性代数·矩阵·音视频