返回介绍

solution / 2100-2199 / 2133.Check if Every Row and Column Contains All Numbers / README_EN

发布于 2024-06-17 01:03:09 字数 4851 浏览 0 评论 0 收藏 0

2133. Check if Every Row and Column Contains All Numbers

中文文档

Description

An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).

Given an n x n integer matrix matrix, return true _if the matrix is valid._ Otherwise, return false.

 

Example 1:

Input: matrix = [[1,2,3],[3,1,2],[2,3,1]]
Output: true
Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3.
Hence, we return true.

Example 2:

Input: matrix = [[1,1,1],[1,2,3],[1,2,3]]
Output: false
Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3.
Hence, we return false.

 

Constraints:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 100
  • 1 <= matrix[i][j] <= n

Solutions

Solution 1

class Solution:
  def checkValid(self, matrix: List[List[int]]) -> bool:
    n = len(matrix)
    for i in range(n):
      seen = [False] * n
      for j in range(n):
        v = matrix[i][j] - 1
        if seen[v]:
          return False
        seen[v] = True
    for j in range(n):
      seen = [False] * n
      for i in range(n):
        v = matrix[i][j] - 1
        if seen[v]:
          return False
        seen[v] = True
    return True
class Solution {
  public boolean checkValid(int[][] matrix) {
    int n = matrix.length;
    for (int i = 0; i < n; ++i) {
      boolean[] seen = new boolean[n];
      for (int j = 0; j < n; ++j) {
        int v = matrix[i][j] - 1;
        if (seen[v]) {
          return false;
        }
        seen[v] = true;
      }
    }
    for (int j = 0; j < n; ++j) {
      boolean[] seen = new boolean[n];
      for (int i = 0; i < n; ++i) {
        int v = matrix[i][j] - 1;
        if (seen[v]) {
          return false;
        }
        seen[v] = true;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool checkValid(vector<vector<int>>& matrix) {
    int n = matrix.size();
    for (int i = 0; i < n; ++i) {
      vector<bool> seen(n);
      for (int j = 0; j < n; ++j) {
        int v = matrix[i][j] - 1;
        if (seen[v]) return false;
        seen[v] = true;
      }
    }
    for (int j = 0; j < n; ++j) {
      vector<bool> seen(n);
      for (int i = 0; i < n; ++i) {
        int v = matrix[i][j] - 1;
        if (seen[v]) return false;
        seen[v] = true;
      }
    }
    return true;
  }
};
func checkValid(matrix [][]int) bool {
  n := len(matrix)
  for i := 0; i < n; i++ {
    seen := make([]bool, n)
    for j := 0; j < n; j++ {
      v := matrix[i][j] - 1
      if seen[v] {
        return false
      }
      seen[v] = true
    }
  }
  for j := 0; j < n; j++ {
    seen := make([]bool, n)
    for i := 0; i < n; i++ {
      v := matrix[i][j] - 1
      if seen[v] {
        return false
      }
      seen[v] = true
    }
  }
  return true
}
function checkValid(matrix: number[][]): boolean {
  const n = matrix.length;
  let rows = Array.from({ length: n }, () => new Array(n).fill(false));
  let cols = Array.from({ length: n }, () => new Array(n).fill(false));
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      let cur = matrix[i][j];
      if (rows[i][cur] || cols[j][cur]) return false;
      rows[i][cur] = true;
      cols[j][cur] = true;
    }
  }
  return true;
}

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

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

发布评论

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