class Solution {
public:
int countServers(vector<vector<int>>& grid) {
}
};
1267. 统计参与通信的服务器
这里有一幅服务器分布图,服务器的位置标识在 m * n
的整数矩阵网格 grid
中,1 表示单元格上有服务器,0 表示没有。
如果两台服务器位于同一行或者同一列,我们就认为它们之间可以进行通信。
请你统计并返回能够与至少一台其他服务器进行通信的服务器的数量。
示例 1:
输入:grid = [[1,0],[0,1]] 输出:0 解释:没有一台服务器能与其他服务器进行通信。
示例 2:
输入:grid = [[1,0],[1,1]] 输出:3 解释:所有这些服务器都至少可以与一台别的服务器进行通信。
示例 3:
输入:grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] 输出:4 解释:第一行的两台服务器互相通信,第三列的两台服务器互相通信,但右下角的服务器无法与其他服务器通信。
提示:
m == grid.length
n == grid[i].length
1 <= m <= 250
1 <= n <= 250
grid[i][j] == 0 or 1
原站题解
php 解法, 执行用时: 148 ms, 内存消耗: 25.4 MB, 提交时间: 2023-08-24 09:34:53
class Solution { /** * @param Integer[][] $grid * @return Integer */ function countServers($grid) { $m = sizeof($grid); $n = sizeof($grid[0]); $count_m = array_fill(0, $m, 0); $count_n = array_fill(0, $n, 0); for ( $i = 0; $i < $m; $i++ ) { for ( $j = 0; $j < $n; $j++ ) { if ( $grid[$i][$j] == 1 ) { $count_m[$i]++; $count_n[$j]++; } } } $ans = 0; for ( $i = 0; $i < $m; $i++ ) { for ( $j = 0; $j < $n; $j++ ) { if ( $grid[$i][$j] == 1 and ($count_m[$i] > 1 or $count_n[$j] > 1) ) { $ans++; } } } return $ans; } }
cpp 解法, 执行用时: 48 ms, 内存消耗: 22 MB, 提交时间: 2023-08-24 07:44:20
class Solution { public: int countServers(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); unordered_map<int, int> rows, cols; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { ++rows[i]; ++cols[j]; } } } int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1 && (rows[i] > 1 || cols[j] > 1)) { ++ans; } } } return ans; } };
java 解法, 执行用时: 5 ms, 内存消耗: 47.4 MB, 提交时间: 2023-08-24 07:43:53
class Solution { public int countServers(int[][] grid) { int m = grid.length, n = grid[0].length; Map<Integer, Integer> rows = new HashMap<Integer, Integer>(); Map<Integer, Integer> cols = new HashMap<Integer, Integer>(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { rows.put(i, rows.getOrDefault(i, 0) + 1); cols.put(j, cols.getOrDefault(j, 0) + 1); } } } int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1 && (rows.get(i) > 1 || cols.get(j) > 1)) { ++ans; } } } return ans; } }
golang 解法, 执行用时: 44 ms, 内存消耗: 6.8 MB, 提交时间: 2022-12-04 11:36:21
func countServers(grid [][]int) int { if len(grid) == 0 { return 0 } countH := make([]int, len(grid)) countL := make([]int, len(grid[0])) for i1, v1 := range grid { for i2, v2 := range v1 { if v2 == 1 { countH[i1]++ countL[i2]++ } } } var res int for i1, v1 := range grid { for i2, v2 := range v1 { if v2 == 1 && (countH[i1] > 1 || countL[i2] > 1) { res++ } } } return res }
golang 解法, 执行用时: 60 ms, 内存消耗: 7.5 MB, 提交时间: 2022-12-04 11:35:35
func countServers(grid [][]int) int { m, n := len(grid), len(grid[0]) var dfs func(x, y int) int dfs = func(x, y int) int { if grid[x][y] == 0 { return 0 } grid[x][y] = 0 r := 1 for tx := 0; tx < m; tx++ { r += dfs(tx, y) } for ty := 0; ty < n; ty++ { r += dfs(x, ty) } return r } var r int for i := range grid { for j := range grid[i] { d := dfs(i,j) if d > 1 { r += d } } } return r }
python3 解法, 执行用时: 96 ms, 内存消耗: 15.9 MB, 提交时间: 2022-12-04 11:34:26
class Solution: def countServers(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) count_m, count_n = [0] * m, [0] * n for i in range(m): for j in range(n): if grid[i][j] == 1: count_m[i] += 1 count_n[j] += 1 ans = 0 for i in range(m): for j in range(n): if grid[i][j] == 1 and (count_m[i] > 1 or count_n[j] > 1): ans += 1 return ans