{"id":7327,"date":"2020-09-05T10:44:45","date_gmt":"2020-09-05T17:44:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7327"},"modified":"2020-09-05T10:45:16","modified_gmt":"2020-09-05T17:45:16","slug":"leetcode-1572-matrix-diagonal-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1572-matrix-diagonal-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1572. Matrix Diagonal Sum"},"content":{"rendered":"\n<p>Given a&nbsp;square&nbsp;matrix&nbsp;<code>mat<\/code>, return the sum of the matrix diagonals.<\/p>\n\n\n\n<p>Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/14\/sample_1911.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[<strong>1<\/strong>,2,<strong>3<\/strong>],\n&nbsp;             [4,<strong>5<\/strong>,6],\n&nbsp;             [<strong>7<\/strong>,8,<strong>9<\/strong>]]\n<strong>Output:<\/strong> 25\n<strong>Explanation: <\/strong>Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25\nNotice that element mat[1][1] = 5 is counted only once.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[<strong>1<\/strong>,1,1,<strong>1<\/strong>],\n&nbsp;             [1,<strong>1<\/strong>,<strong>1<\/strong>,1],\n&nbsp;             [1,<strong>1<\/strong>,<strong>1<\/strong>,1],\n&nbsp;             [<strong>1<\/strong>,1,1,<strong>1<\/strong>]]\n<strong>Output:<\/strong> 8\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[<strong>5<\/strong>]]\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == mat.length == mat[i].length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>1 &lt;= mat[i][j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Note: if n is odd, be careful not to double count the center one.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int diagonalSum(vector<vector<int>>& mat) {\n    const int n = mat.size();\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      ans += mat[i][i] + mat[i][n - i - 1];\n    if (n &#038; 1) ans -= mat[n \/ 2][n \/ 2];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;square&nbsp;matrix&nbsp;mat, return the sum of the matrix diagonals. Only include the sum of all the elements on the primary diagonal and all the elements&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[535,222,216],"class_list":["post-7327","post","type-post","status-publish","format-standard","hentry","category-array","tag-diagonal","tag-easy","tag-matrix","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7327"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7327\/revisions"}],"predecessor-version":[{"id":7329,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7327\/revisions\/7329"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}