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

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

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

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

相关推荐
szekl2 小时前
HDMI 2.0 4×2矩阵切换器412HN——多信号输入输出的高清解决方案
linux·矩阵·计算机外设·电脑·ekl
盛寒20 小时前
矩阵的定义和运算 线性代数
线性代数
盛寒20 小时前
初等变换 线性代数
线性代数
叶子爱分享1 天前
浅谈「线性代数的本质」 - 系列合集
线性代数
luofeiju1 天前
RGB下的色彩变换:用线性代数解构色彩世界
图像处理·人工智能·opencv·线性代数
好开心啊没烦恼1 天前
Python:线性代数,向量内积谐音记忆。
开发语言·python·线性代数·数据挖掘·数据分析
引量AI11 天前
TikTok 矩阵如何快速涨粉
大数据·人工智能·矩阵·tiktok矩阵·海外社媒
Ven%11 天前
矩阵阶数(线性代数) vs. 张量维度(深度学习):线性代数与深度学习的基石辨析,再也不会被矩阵阶数给混淆了
人工智能·pytorch·深度学习·线性代数·矩阵·tensor·张量
云云32111 天前
亚矩云手机赋能Vinted矩阵运营:破解二手电商多账号与本地化困局
网络·人工智能·智能手机·矩阵·自动化
云云32112 天前
Subway Surfers Blast × 亚矩阵云手机:手游矩阵运营的终极变现方案
大数据·人工智能·线性代数·智能手机·矩阵·架构