当前位置: 代码迷 >> 综合 >> Leetcode 1267. Count Servers that Communicate
  详细解决方案

Leetcode 1267. Count Servers that Communicate

热度:74   发布时间:2023-12-12 21:03:58.0

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Count Servers that Communicate

2. Solution

**解析:**Version 1,统计行和和列和,然后逐个判断。

  • Version 1
class Solution:def countServers(self, grid: List[List[int]]) -> int:m = len(grid)n = len(grid[0])# rowSum = [sum(grid[i]) for i in range(m)]# colSum = [sum(x) for x in zip(*grid)]rowSum = [0] * mcolSum = [0] * nfor i in range(m):for j in range(n):if grid[i][j] == 1:rowSum[i] += 1colSum[j] += 1count = 0for i in range(m):for j in range(n):if grid[i][j] == 1 and (rowSum[i] > 1 or colSum[j] > 1):count += 1return count

Reference

  1. https://leetcode.com/problems/count-servers-that-communicate/
  相关解决方案