给你一个大小为 rows x cols
的矩阵 mat
,其中 mat[i][j]
是 0
或 1
,请返回 矩阵 mat
中特殊位置的数目 。
特殊位置 定义:如果 mat[i][j] == 1
并且第 i
行和第 j
列中的所有其他元素均为 0
(行和列的下标均 从 0 开始 ),则位置 (i, j)
被称为特殊位置。
示例 1:
输入:mat = [[1,0,0], [0,0,1], [1,0,0]] 输出:1 解释:(1,2) 是一个特殊位置,因为 mat[1][2] == 1 且所处的行和列上所有其他元素都是 0
示例 2:
输入:mat = [[1,0,0], [0,1,0], [0,0,1]] 输出:3 解释:(0,0), (1,1) 和 (2,2) 都是特殊位置
示例 3:
输入:mat = [[0,0,0,1], [1,0,0,0], [0,1,1,0], [0,0,0,0]] 输出:2
示例 4:
输入:mat = [[0,0,0,0,0], [1,0,0,0,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,1,1]] 输出:3
提示:
rows == mat.length
cols == mat[i].length
1 <= rows, cols <= 100
mat[i][j]
是0
或1
方法一:模拟
遍历矩阵 mat
,先统计每一行,每一列中 1
的个数,分别记录在 r
和 c
数组中。
然后再遍历矩阵 mat
,如果 mat[i][j] == 1
且 row[i] == 1
且 col[j] == 1
,则
时间复杂度 mat
的行数和列数。
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
}
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;
}
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
}
}