mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
61 lines
3.9 KiB
HTML
61 lines
3.9 KiB
HTML
|
<p>给你一个下标从 <strong>0</strong> 开始的 <code>m x n</code> 二进制矩阵 <code>grid</code> 。</p>
|
|||
|
|
|||
|
<p>我们按照如下过程,定义一个下标从 <strong>0</strong> 开始的 <code>m x n</code> 差值矩阵 <code>diff</code> :</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>令第 <code>i</code> 行一的数目为 <code>onesRow<sub>i</sub></code> 。</li>
|
|||
|
<li>令第 <code>j</code> 列一的数目为 <code>onesCol<sub>j</sub></code><sub> </sub>。</li>
|
|||
|
<li>令第 <code>i</code> 行零的数目为 <code>zerosRow<sub>i</sub></code> 。</li>
|
|||
|
<li>令第 <code>j</code> 列零的数目为 <code>zerosCol<sub>j</sub></code> 。</li>
|
|||
|
<li><code>diff[i][j] = onesRow<sub>i</sub> + onesCol<sub>j</sub> - zerosRow<sub>i</sub> - zerosCol<sub>j</sub></code></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p>请你返回差值矩阵<em> </em><code>diff</code> 。</p>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>示例 1:</strong></p>
|
|||
|
|
|||
|
<p><img src="https://assets.leetcode.com/uploads/2022/11/06/image-20221106171729-5.png" style="width: 400px; height: 208px;"></p>
|
|||
|
|
|||
|
<pre><b>输入:</b>grid = [[0,1,1],[1,0,1],[0,0,1]]
|
|||
|
<b>输出:</b>[[0,0,4],[0,0,4],[-2,-2,2]]
|
|||
|
<b>解释:</b>
|
|||
|
- diff[0][0] = <code>onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0
|
|||
|
- diff[0][1] = <code>onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0
|
|||
|
- diff[0][2] = <code>onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4
|
|||
|
- diff[1][0] = <code>onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0
|
|||
|
- diff[1][1] = <code>onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0
|
|||
|
- diff[1][2] = <code>onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4
|
|||
|
- diff[2][0] = <code>onesRow<sub>2</sub> + onesCol<sub>0</sub> - zerosRow<sub>2</sub> - zerosCol<sub>0</sub></code> = 1 + 1 - 2 - 2 = -2
|
|||
|
- diff[2][1] = <code>onesRow<sub>2</sub> + onesCol<sub>1</sub> - zerosRow<sub>2</sub> - zerosCol<sub>1</sub></code> = 1 + 1 - 2 - 2 = -2
|
|||
|
- diff[2][2] = <code>onesRow<sub>2</sub> + onesCol<sub>2</sub> - zerosRow<sub>2</sub> - zerosCol<sub>2</sub></code> = 1 + 3 - 2 - 0 = 2
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 2:</strong></p>
|
|||
|
|
|||
|
<p><img src="https://assets.leetcode.com/uploads/2022/11/06/image-20221106171747-6.png" style="width: 358px; height: 150px;"></p>
|
|||
|
|
|||
|
<pre><b>输入:</b>grid = [[1,1,1],[1,1,1]]
|
|||
|
<b>输出:</b>[[5,5,5],[5,5,5]]
|
|||
|
<strong>解释:</strong>
|
|||
|
- diff[0][0] = onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5
|
|||
|
- diff[0][1] = onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5
|
|||
|
- diff[0][2] = onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5
|
|||
|
- diff[1][0] = onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5
|
|||
|
- diff[1][1] = onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5
|
|||
|
- diff[1][2] = onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5
|
|||
|
</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 <= m, n <= 10<sup>5</sup></code></li>
|
|||
|
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
|||
|
<li><code>grid[i][j]</code> 要么是 <code>0</code> ,要么是 <code>1</code> 。</li>
|
|||
|
</ul>
|