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

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

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

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

相关推荐
AI科技星13 小时前
哥德巴赫猜想1+1基于平行素数对等腰梯形网格拓扑与素数渐近密度的大偶数满填充完备性证明
人工智能·线性代数·架构·概率论·学习方法
大大杰哥18 小时前
leetcode hot100(4)矩阵
算法·leetcode·矩阵
2601_9577867719 小时前
多平台矩阵系统的反脆弱架构:如何用技术解耦对抗平台规则的不确定性
人工智能·矩阵·架构·平台解耦
2601_9577875820 小时前
智能矩阵运营系统的流量博弈论:当1000个账号争夺有限流量时,最优调度策略是什么?
人工智能·矩阵·流量调度·智能矩阵运营系统
2601_9577867721 小时前
企业级矩阵系统架构深度解析:从冷启动到规模化增长的技术演进
矩阵·系统架构·内容矩阵
2601_957787581 天前
矩阵运营的技术底座:为什么“一体化系统“正在取代“工具拼装“
人工智能·矩阵·矩阵运营
AI科技星1 天前
《数学公理体系·第三部·数术几何》(2026 年版)
c语言·开发语言·线性代数·算法·矩阵·量子计算·agi
feifeigo1232 天前
STM32矩阵键盘驱动(库函数版)实现
stm32·矩阵·计算机外设
oo哦哦2 天前
全域矩阵系统的技术架构拆解:从单点效率到链路闭环
人工智能·矩阵·架构
2601_957786772 天前
拆解矩阵系统的底层逻辑:从“人海战术“到“一套系统管所有“
大数据·人工智能·矩阵