返回介绍

solution / 1500-1599 / 1582.Special Positions in a Binary Matrix / README_EN

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

1582. Special Positions in a Binary Matrix

中文文档

Description

Given an m x n binary matrix mat, return _the number of special positions in _mat_._

A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).

 

Example 1:

Input: mat = [[1,0,0],[0,0,1],[1,0,0]]
Output: 1
Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.

Example 2:

Input: mat = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Explanation: (0, 0), (1, 1) and (2, 2) are special positions.

 

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 100
  • mat[i][j] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def numSpecial(self, mat: List[List[int]]) -> int:
    m, n = len(mat), len(mat[0])
    r = [0] * m
    c = [0] * n
    for i, row in enumerate(mat):
      for j, v in enumerate(row):
        r[i] += v
        c[j] += v
    ans = 0
    for i in range(m):
      for j in range(n):
        if mat[i][j] == 1 and r[i] == 1 and c[j] == 1:
          ans += 1
    return ans
class Solution {
  public int numSpecial(int[][] mat) {
    int m = mat.length, n = mat[0].length;
    int[] r = new int[m];
    int[] c = new int[n];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        r[i] += mat[i][j];
        c[j] += mat[i][j];
      }
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (mat[i][j] == 1 && r[i] == 1 && c[j] == 1) {
          ++ans;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int numSpecial(vector<vector<int>>& mat) {
    int m = mat.size(), n = mat[0].size();
    vector<int> r(m), c(n);
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        r[i] += mat[i][j];
        c[j] += mat[i][j];
      }
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (mat[i][j] == 1 && r[i] == 1 && c[j] == 1) {
          ++ans;
        }
      }
    }
    return ans;
  }
};
func numSpecial(mat [][]int) int {
  m, n := len(mat), len(mat[0])
  r, c := make([]int, m), make([]int, n)
  for i, row := range mat {
    for j, v := range row {
      r[i] += v
      c[j] += v
    }
  }
  ans := 0
  for i, x := range r {
    for j, y := range c {
      if mat[i][j] == 1 && x == 1 && y == 1 {
        ans++
      }
    }
  }
  return ans
}
function numSpecial(mat: number[][]): number {
  const m = mat.length;
  const n = mat[0].length;
  const rows = new Array(m).fill(0);
  const cols = new Array(n).fill(0);
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (mat[i][j] === 1) {
        rows[i]++;
        cols[j]++;
      }
    }
  }

  let res = 0;
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (mat[i][j] === 1 && rows[i] === 1 && cols[j] === 1) {
        res++;
      }
    }
  }

  return res;
}
impl Solution {
  pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {
    let m = mat.len();
    let n = mat[0].len();
    let mut rows = vec![0; m];
    let mut cols = vec![0; n];
    for i in 0..m {
      for j in 0..n {
        rows[i] += mat[i][j];
        cols[j] += mat[i][j];
      }
    }

    let mut res = 0;
    for i in 0..m {
      for j in 0..n {
        if mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1 {
          res += 1;
        }
      }
    }
    res
  }
}
int numSpecial(int** mat, int matSize, int* matColSize) {
  int m = matSize;
  int n = *matColSize;
  int* rows = (int*) malloc(sizeof(int) * m);
  int* cols = (int*) malloc(sizeof(int) * n);
  memset(rows, 0, sizeof(int) * m);
  memset(cols, 0, sizeof(int) * n);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (mat[i][j] == 1) {
        rows[i]++;
        cols[j]++;
      }
    }
  }
  int res = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1) {
        res++;
      }
    }
  }
  free(rows);
  free(cols);
  return res;
}

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

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

发布评论

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