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

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

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

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

相关推荐
痛&快乐着8 小时前
衡量矩阵数值稳定性的关键指标:矩阵的条件数
线性代数·矩阵
CoderCodingNo19 小时前
【GESP】C++二级真题 luogu-B4259 [GESP202503 二级] 等差矩阵
java·c++·矩阵
悲喜自渡72121 小时前
线性代数(一些别的应该关注的点)
python·线性代数·机器学习
星云ai2 天前
矩阵运营:抢占市场与流量的利器
矩阵
太妃糖耶2 天前
URP-利用矩阵在Shader中实现物体的平移和缩放
unity·矩阵
优美的赫蒂3 天前
理解欧拉公式
线性代数·算法·数学建模
岩中竹3 天前
力扣热题100题解(c++)—矩阵
数据结构·c++·程序人生·算法·leetcode·矩阵
byxdaz3 天前
矩阵运算和线性代数操作开源库
矩阵
User_芊芊君子3 天前
【C语言经典算法实战】:从“移动距离”问题看矩阵坐标计算
c语言·算法·矩阵
weixin_428498493 天前
使用HYPRE库并行装配IJ稀疏矩阵
线性代数·矩阵