https://leetcode.com/problems/special-positions-in-a-binary-matrix/description
1582. Special Positions in a Binary Matrix
給你一個二維陣列表示的矩陣mat,如果一個點他的上下左右行和列只有0那這個點是一個
Special Position,找出共有幾個Special Position。
思路:
1.如果一個點是1就用迴圈檢查他那一行和列,都是0就遞增。
Java Code:
---------------------------------------------------------------
class Solution {
public int numSpecial(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1 && isSpecial(mat, i, j)) {
res++;
}
}
}
return res;
}
private boolean isSpecial(int[][] mat, int y, int x) {
int m = mat.length;
int n = mat[0].length;
for (int i = 0; i < m; i++) {
if (mat[i][x] == 1 && i != y) {
return false;
}
}
for (int i = 0; i < n; i++) {
if (mat[y][i] == 1 && i != x) {
return false;
}
}
return true;
}
}
---------------------------------------------------------------
--
https://i.imgur.com/DANRJFR.jpg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.69.212 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1702446165.A.B5F.html