题目如下:

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.

Example 1:

Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.

Example 2:

Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.

Example 3:

Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate
with each other. The server at right bottom corner can't communicate with any other server. 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m <= 250
  • 1 <= n <= 250
  • grid[i][j] == 0 or 1

解题思路:和求岛的个数,最大岛等题目的解法一样,DFS或者BFS。

代码如下:

class Solution(object):
def countServers(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
res = 0
visit = [[0] * len(grid[0]) for _ in grid]
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 0 or visit[i][j] == 1:
continue
queue = [(i,j)]
visit[i][j] = 1
group = 0
while len(queue) > 0:
x,y = queue.pop(0)
group += 1
for k in range(len(grid[x])):
if grid[x][k] == 1 and visit[x][k] == 0:
queue.append((x,k))
visit[x][k] = 1
for k in range(len(grid)):
if grid[k][y] == 1 and visit[k][y] == 0:
queue.append((k,y))
visit[k][y] = 1
if group > 1:res += group
return res

【leetcode】1267. Count Servers that Communicate的更多相关文章

  1. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  2. leetcode 1267. Count Servers that Communicate

    You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means th ...

  3. 【LeetCode】 204. Count Primes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...

  4. 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...

  5. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

  6. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】38 - Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  8. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  9. 【Leetcode】357. Count Numbers with Unique Digits

    题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...

随机推荐

  1. 【转载】softmax的性质及其实现

    原文地址:https://segmentfault.com/a/1190000010039529?utm_source=tag-newest softmax函数将任意n维的实值向量转换为取值范围在(0 ...

  2. sqlalchemy映射数据库

    from sqlalchemy import create_engine,Column,Integer,String from sqlalchemy.ext.declarative import de ...

  3. make j* make j4 make j8 区别

    转载: make -j4是什么意思看书上说1) make(1)只衍生一个作业//作业是什么意思?make(1) 是不是就是make的意思?2) 在双处理器上make -j4,难道是让每个处理器跑两个作 ...

  4. Python环境配置问题及解决办法

    Windows下用pip安装包时出现"error: Microsoft Visual C++ 9.0 is required"错误 error: Microsoft Visual ...

  5. 为什么 ConcurrentHashMap 的读操作不需要加锁?

    现在人工智能非常火爆,很多朋友都想学,但是一般的教程都是为博硕生准备的,太难看懂了.最近发现了一个非常适合小白入门的教程,不仅通俗易懂而且还很风趣幽默.所以忍不住分享一下给大家 ConcurrentH ...

  6. 记一次完整的java项目压力测试

    总结:通过这次压力测试,增加了对程序的理解:假定正常情况下方法执行时间为2秒,吞吐量为100/s,则并发为200/s:假设用户可接受范围为10s,那么并发量可以继续增加到1000/s,到这个时候一切还 ...

  7. C语言No such file or directory错误

    昨天晚上因为这个错误,都没睡好觉 早上六点起来查资料,换了个绝对路径就行了 #include"D:\软工专业\数据结构PPT和作业\实验作业\实验上机\单链表的基本操作\HeadFile.h ...

  8. 常用的TCP/UDP端口

    已知的TCP/UDP端口可以在wikipedia上找到: List of TCP and UDP port numbers, 太多了,按组列举了最常用的,如下: FTP:21SSH:22Telnet: ...

  9. 099、如何访问Service (Swarm06)

    参考https://www.cnblogs.com/CloudMan6/p/7909136.html   前面已经学习了如何部署Service吗,也验证了swarm的failover特性,下面我们要学 ...

  10. springboot(二十)-配置文件 bootstrap和application区别

    用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件 bootstrap (.yml 或者 .properties) application (.yml 或者 .pr ...