1267. Count Servers that Communicate
- Count Servers that Communicatepython solution
题目描述
You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.
Return the number of servers that communicate with any other server.

解析
遍历统计,按行和按列。只要有同行或同列满足count_X>1即可。
class Solution:
def countServers(self, grid: List[List[int]]) -> int:
count=0
row=len(grid)
col=len(grid[0])
count_row=row*[0]
count_col=col*[0]
for i in range(row):
for j in range(col):
if grid[i][j]==1:
count_row[i]+=1
count_col[j]+=1
for i in range(row):
for j in range(col):
if grid[i][j]==1 and (count_row[i]>1 or count_col[j]>1):
count+=1
return count
本文介绍了一种算法,用于计算在给定的服务器中心地图中,能够与其他服务器进行通信的服务器数量。通过遍历统计行和列中服务器的数量,确定哪些服务器可以进行通信。
1474

被折叠的 条评论
为什么被折叠?



