链接:
题意:
同行同列可以发生通信,求能发生通信的机器数量
解:
标记每行/每列的机器个数即可
实际代码:
c++
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int countServers(vector<vector<int>>& grid) {
int row=grid.size(),col=grid[0].size();
vector<int>rowNum(row),colNum(col);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(grid[i][j])
{
rowNum[i]++;
colNum[j]++;
}
}
}
int ans=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if((grid[i][j])&&(rowNum[i]>1||colNum[j]>1)) ans++;
}
}
return ans;
}
};
int main()
{
Solution solution;
}
限制:
m == grid.length
n == grid[i].length
1 <= m <= 250
1 <= n <= 250
grid[i][j] == 0 or 1