1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-25 17:50:26 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最大的幻方 [largest-magic-square].html

34 lines
1.5 KiB
HTML
Raw Normal View History

2022-03-27 20:37:52 +08:00
<p>一个 <code>k x k</code> 的<strong> 幻方</strong> 指的是一个 <code>k x k</code> 填满整数的方格阵,且每一行、每一列以及两条对角线的和 <strong>全部</strong><strong>相等</strong> 。幻方中的整数 <strong>不需要互不相同</strong> 。显然,每个 <code>1 x 1</code> 的方格都是一个幻方。</p>
<p>给你一个 <code>m x n</code> 的整数矩阵 <code>grid</code> ,请你返回矩阵中 <strong>最大幻方</strong> 的 <strong>尺寸</strong> (即边长 <code>k</code>)。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare-grid.jpg" style="width: 413px; height: 335px;">
<pre><b>输入:</b>grid = [[7,1,4,5,6],[2,5,1,6,4],[1,5,4,3,2],[1,2,7,3,4]]
<b>输出:</b>3
<b>解释:</b>最大幻方尺寸为 3 。
每一行,每一列以及两条对角线的和都等于 12 。
- 每一行的和5+1+6 = 5+4+3 = 2+7+3 = 12
- 每一列的和5+5+2 = 1+4+7 = 6+3+3 = 12
- 对角线的和5+4+3 = 6+4+2 = 12
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare2-grid.jpg" style="width: 333px; height: 255px;">
<pre><b>输入:</b>grid = [[5,1,3,1],[9,3,3,1],[1,3,3,8]]
<b>输出:</b>2
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 50</code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code></li>
</ul>