当前位置: 首页 > news >正文

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

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

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 mat
        int 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 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.

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

相关文章:

  • 科技快讯 | 全球首个通用智能人“通通”2.0正式发布;X 平台被曝遭遇严重数据泄露;智谱发布Agent产品AutoGLM沉思 可以像人类一样
  • leetcode刷题日记——接雨水
  • 4.1学习总结 拼图小游戏+集合进阶
  • Oracle迁移达梦遇中断?试试SQLark的断点续迁功能!
  • 代码随想录算法训练营第三十四天 | 62.不同路径 63.不同路径II 343.整数拆分
  • Springboot集成Dubbo和Zookeeper框架搭建
  • 基于 Vue + Django + MySQL 实现个人博客/CMS系统
  • 基于单片机的音乐播放器系统设计
  • FPGA学习-基于 DE2-115 板的 Verilog 分秒计数器设计与按键功能实现
  • 第一章 EDA技术概述
  • NLP高频面试题(三十)——LLama系列模型介绍,包括LLama LLama2和LLama3
  • AI原生应用爆发:从通用大模型到垂直场景的算力重构
  • C++ --- map和set的使用
  • 【Linux】高性能网络模式:Reactor 反应堆模式
  • 搞 PostgreSQL多才多艺的人--赵渝强 《PG数据库实战派》
  • 【容器】设备上没有剩余空间的错误排查处理
  • flutter WEB端启动优化(加载速度,加载动画)
  • ubuntu虚拟机裁剪img文件系统
  • WGAN的对偶性理解
  • Mybatis源码分析
  • 为何发胖?如何减肥?一个医学体重管理中心的探索启示
  • 重庆大学通报本科生发14篇SCI论文:涉事学生及其父亲被处理
  • 匈牙利外长称匈方已驱逐两名乌克兰外交官
  • AI药企英矽智能第三次递表港交所:去年亏损超1700万美元,收入多数来自对外授权
  • 两部门部署中小学幼儿园教师招聘工作:吸纳更多高校毕业生从教
  • 深入贯彻中央八项规定精神学习教育中央第一指导组指导督导河北省见面会召开