mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
32 lines
1.9 KiB
HTML
32 lines
1.9 KiB
HTML
<p>You are given a <strong>0-indexed</strong> 2D integer matrix <code><font face="monospace">grid</font></code> of size <code>n * n</code> with values in the range <code>[1, n<sup>2</sup>]</code>. Each integer appears <strong>exactly once</strong> except <code>a</code> which appears <strong>twice</strong> and <code>b</code> which is <strong>missing</strong>. The task is to find the repeating and missing numbers <code>a</code> and <code>b</code>.</p>
|
|
|
|
<p>Return <em>a <strong>0-indexed </strong>integer array </em><code>ans</code><em> of size </em><code>2</code><em> where </em><code>ans[0]</code><em> equals to </em><code>a</code><em> and </em><code>ans[1]</code><em> equals to </em><code>b</code><em>.</em></p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> grid = [[1,3],[2,2]]
|
|
<strong>Output:</strong> [2,4]
|
|
<strong>Explanation:</strong> Number 2 is repeated and number 4 is missing so the answer is [2,4].
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> grid = [[9,1,7],[8,9,2],[3,4,6]]
|
|
<strong>Output:</strong> [9,5]
|
|
<strong>Explanation:</strong> Number 9 is repeated and number 5 is missing so the answer is [9,5].
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>2 <= n == grid.length == grid[i].length <= 50</code></li>
|
|
<li><code>1 <= grid[i][j] <= n * n</code></li>
|
|
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> there is exactly one <code>x</code> that is not equal to any of the grid members.</li>
|
|
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> there is exactly one <code>x</code> that is equal to exactly two of the grid members.</li>
|
|
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> except two of them there is exatly one pair of <code>i, j</code> that <code>0 <= i, j <= n - 1</code> and <code>grid[i][j] == x</code>.</li>
|
|
</ul>
|