[LeetCode] 1582 - Special Positions in a Binary Matrix
문제
- 링크: https://leetcode.com/problems/special-positions-in-a-binary-matrix
- 난이도: Easy
- 태그: 배열
- 결과:
Time: 0 ms (100%), Space: 16.6 MB (73.7%)
풀이
class Solution {
public:
int numSpecial(vector<vector<int>>& mat)
{
int ans = 0;
int m = mat.size();
int n = mat[0].size();
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (mat[i][j] != 1)
{
continue;
}
bool valid = true;
for (int r = 0; r < m; r++)
{
if (r == i)
{
continue;
}
if (mat[r][j] == 1)
{
valid = false;
break;
}
}
for (int c = 0; c < n; c++)
{
if (c == j)
{
continue;
}
if (mat[i][c] == 1)
{
valid = false;
break;
}
}
if (valid)
{
ans++;
}
}
}
return ans;
}
};
그냥 하나하나 구해도 충분히 빠르다.
댓글남기기