LeetCode 1582. 二进制矩阵中的特殊位置
题目描述
给你一个大小为 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
二进制矩阵中的特殊位置
提示:
rows == mat.length
cols == mat[i].length
1 <= rows, cols <= 100
mat[i][j] 是 0 或 1
一、解题关键词
1、数组 2、遍历 3、存储特殊位置坐标
二、解题报告
1.思路分析
特殊位置坐标需要记下来。
双层遍历
2.时间复杂度
3.代码示例
class Solution {
public int numSpecial(int[][] mat) {
int count = 0;
int rowLen = mat.length,colLen = mat[0].length;
int [] row = new int[rowLen];
int [] col = new int[colLen];
for(int i = 0;i < rowLen; i++){
for(int j = 0; j < colLen;j++){
if(mat[i][j] == 1){
row[i]++;
col[j]++;
}
}
}
for(int i = 0;i < rowLen; i++){
for(int j = 0;j < colLen;j++){
if(mat[i][j] == 1 && row[i] == 1 && col[j] == 1){
count ++;
}
}
}
return count;
}
}
2.知识点