【leetcode】1267. Count Servers that Communicate
题目如下:
You are given a map of a server center, represented as a
m * ninteger matrixgrid, 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.lengthn == grid[i].length1 <= m <= 2501 <= n <= 250grid[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的更多相关文章
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 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 ...
- 【LeetCode】 204. Count Primes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】38 - Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
随机推荐
- 修改iframe内元素的样式
$('iframe').load(function () { var x = document.getElementsByTagName('iframe')[0]; var y = (x.cont ...
- JS延迟加载的几种方式
参考链接:https://blog.csdn.net/meijory/article/details/76389762
- C# async await的使用
async 声明一个包含异步代码的函数,该函数执行时不会阻塞调用线程. async标记的函数返回值必须为 void ,Task,Task<TResult> await 必须修饰Task 或 ...
- 【python基础学习】---解析多层json,解析xml
1.以豆瓣的API接口为例子,解析返回的json数据 https://api.douban.com/v2/book/1220502 { "rating":{ "max&q ...
- 解决VS2008之后平台(如VS2012/VS2013/VS2015)调试模式下不显示主界面窗口的问题
问题描述:win10操作系统下,VS2008工程调试模式下正常显示主界面窗口,使用VS2012/VS2013/VS2015环境打开VS2008工程,调试模式下应用程序转为后台进程,不显示主界面窗口:另 ...
- SpringSecurity 配置
SpringSecurity+JWT https://www.jianshu.com/p/5b9f1f4de88d https://blog.csdn.net/qq_35494808/article/ ...
- learning_git_from_Liao
安装 windows git 直接去官网就行,地址如下: https://git-scm.com 安装完成后,在开始菜单里找到"Git"->"Git Bash&qu ...
- Spread.NET 表格控件 V12.0 Update2 发布更新
Spread.NET表格控件V12.0 Update 2 已经正式发布,本次发布主要针对WinForm平台下客户反馈的产品使用功能进行优化,并修复了已知问题,具体修复情况见下方说明. Spread.N ...
- 小菜鸟之java JDBC编程
JDBC技术 百度简介 : JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一 ...
- python-bioInfo-codes-2
1. _tkinter.TclError: no display name and no $DISPLAY environment variable 解决方案: import matplotlibma ...


