您的位置:首页 > 财经 > 金融 > 海关数据查询平台官网_一个完整的活动策划方案范文_seo关键词排名优化如何_网页制作工具有哪些

海关数据查询平台官网_一个完整的活动策划方案范文_seo关键词排名优化如何_网页制作工具有哪些

2025/6/20 18:11:01 来源:https://blog.csdn.net/ElseWhereR/article/details/146909391  浏览:    关键词:海关数据查询平台官网_一个完整的活动策划方案范文_seo关键词排名优化如何_网页制作工具有哪些
海关数据查询平台官网_一个完整的活动策划方案范文_seo关键词排名优化如何_网页制作工具有哪些

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

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.

int w = 13;
int t = 38;

make a one-dimension vector.

// 直接给定数组,数组的名字是自定义的
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. 

// 一维数组
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.

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

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

class Solution {
public:int diagonalSum(vector<vector<int>>& mat) {int n = mat.size(); // get the size of matint 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.

class Solution {
public:int diagonalSum(vector<vector<int>>& mat) {int n = mat.size(); // get the size of matint 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;}
};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com