计算:先遍历一遍矩阵,记录矩阵在行列上服务器的个数。再遍历一遍矩阵,遇到服务器时,如果该行或列上存在服务器个数>1,则它是可以通讯的。

 1 class Solution {
 2     public int countServers(int[][] grid) {
 3         int n=grid.length,m=grid[0].length,res=0;
 4         int[] col=new int[n];
 5         int[] row=new int[m];
 6         for(int i=0;i<n;i++){
 7             for(int j=0;j<m;j++){
 8                 if(grid[i][j]==1){
 9                     col[i]++;
10                     row[j]++;
11                 }
12             }
13         }
14         for(int i=0;i<n;i++){
15             for(int j=0;j<m;j++){
16                 if(grid[i][j]==1&&(col[i]>1||row[j]>1)) res++;
17             }
18         }
19         return res;
20     }
21 }
posted on 2019-12-22 21:36  Chenjin123  阅读(259)  评论(0编辑  收藏  举报