1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public int countServers(int[][] grid) { int m = grid.length, n = grid[0].length; int[] cntRow = new int[m], cntCol = new int[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { ++cntRow[i]; ++cntCol[j]; } } } int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1 && (cntRow[i] > 1 || cntCol[j] > 1)) { ++ans; } } } return ans; } }
|