Leetcode 1267. Count Servers that Communicate

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1267. Count Servers that Communicate相关的知识,希望对你有一定的参考价值。

文章作者: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] * m
        colSum = [0] * n
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    rowSum[i] += 1
                    colSum[j] += 1
        count = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1 and (rowSum[i] > 1 or colSum[j] > 1):
                    count += 1
        return count

Reference

  1. https://leetcode.com/problems/count-servers-that-communicate/

以上是关于Leetcode 1267. Count Servers that Communicate的主要内容,如果未能解决你的问题,请参考以下文章

leetcode1267

LeetCode Algorithm 1267. 统计参与通信的服务器

leetcode-164周赛-1267-统计参与通信的服务器

Joomla 更新失败。错误 1267 排序规则的非法混合

1267 老鼠的旅行

hd 1267