返回介绍

solution / 2900-2999 / 2965.Find Missing and Repeated Values / README_EN

发布于 2024-06-17 01:02:58 字数 5100 浏览 0 评论 0 收藏 0

2965. Find Missing and Repeated Values

中文文档

Description

You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. Each integer appears exactly once except a which appears twice and b which is missing. The task is to find the repeating and missing numbers a and b.

Return _a 0-indexed integer array _ans_ of size _2_ where _ans[0]_ equals to _a_ and _ans[1]_ equals to _b_._

 

Example 1:

Input: grid = [[1,3],[2,2]]
Output: [2,4]
Explanation: Number 2 is repeated and number 4 is missing so the answer is [2,4].

Example 2:

Input: grid = [[9,1,7],[8,9,2],[3,4,6]]
Output: [9,5]
Explanation: Number 9 is repeated and number 5 is missing so the answer is [9,5].

 

Constraints:

  • 2 <= n == grid.length == grid[i].length <= 50
  • 1 <= grid[i][j] <= n * n
  • For all x that 1 <= x <= n * n there is exactly one x that is not equal to any of the grid members.
  • For all x that 1 <= x <= n * n there is exactly one x that is equal to exactly two of the grid members.
  • For all x that 1 <= x <= n * n except two of them there is exatly one pair of i, j that 0 <= i, j <= n - 1 and grid[i][j] == x.

Solutions

Solution 1: Counting

We create an array $cnt$ of length $n^2 + 1$ to count the frequency of each number in the matrix.

Next, we traverse $i \in [1, n^2]$. If $cnt[i] = 2$, then $i$ is the duplicated number, and we set the first element of the answer to $i$. If $cnt[i] = 0$, then $i$ is the missing number, and we set the second element of the answer to $i$.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the side length of the matrix.

class Solution:
  def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
    n = len(grid)
    cnt = [0] * (n * n + 1)
    for row in grid:
      for v in row:
        cnt[v] += 1
    ans = [0] * 2
    for i in range(1, n * n + 1):
      if cnt[i] == 2:
        ans[0] = i
      if cnt[i] == 0:
        ans[1] = i
    return ans
class Solution {
  public int[] findMissingAndRepeatedValues(int[][] grid) {
    int n = grid.length;
    int[] cnt = new int[n * n + 1];
    int[] ans = new int[2];
    for (int[] row : grid) {
      for (int x : row) {
        if (++cnt[x] == 2) {
          ans[0] = x;
        }
      }
    }
    for (int x = 1;; ++x) {
      if (cnt[x] == 0) {
        ans[1] = x;
        return ans;
      }
    }
  }
}
class Solution {
public:
  vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
    int n = grid.size();
    vector<int> cnt(n * n + 1);
    vector<int> ans(2);
    for (auto& row : grid) {
      for (int x : row) {
        if (++cnt[x] == 2) {
          ans[0] = x;
        }
      }
    }
    for (int x = 1;; ++x) {
      if (cnt[x] == 0) {
        ans[1] = x;
        return ans;
      }
    }
  }
};
func findMissingAndRepeatedValues(grid [][]int) []int {
  n := len(grid)
  ans := make([]int, 2)
  cnt := make([]int, n*n+1)
  for _, row := range grid {
    for _, x := range row {
      cnt[x]++
      if cnt[x] == 2 {
        ans[0] = x
      }
    }
  }
  for x := 1; ; x++ {
    if cnt[x] == 0 {
      ans[1] = x
      return ans
    }
  }
}
function findMissingAndRepeatedValues(grid: number[][]): number[] {
  const n = grid.length;
  const cnt: number[] = Array(n * n + 1).fill(0);
  const ans: number[] = Array(2).fill(0);
  for (const row of grid) {
    for (const x of row) {
      if (++cnt[x] === 2) {
        ans[0] = x;
      }
    }
  }
  for (let x = 1; ; ++x) {
    if (cnt[x] === 0) {
      ans[1] = x;
      return ans;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文