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 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
class Solution {
public:
int countServers(vector<vector<int>>& grid) {
vector<int> row(grid.size(), ), col(grid[].size(), );
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[].size(); ++j) {
if (grid[i][j] == ) {
row[i]++;
col[j]++;
}
}
}
int cnt = ;
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[].size(); ++j) {
if ((grid[i][j] == ) && (row[i] > || col[j] > ))
cnt++;
}
}
return cnt;
}
};
leetcode 1267. Count Servers that Communicate的更多相关文章
- 【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 me ...
- LeetCode 5272. 5272. 统计参与通信的服务器 Count Servers that Communicate
地址 https://leetcode-cn.com/problems/count-servers-that-communicate/ 题目描述这里有一幅服务器分布图,服务器的位置标识在 m * n ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- leetcode@ [327] Count of Range Sum (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 730 Count Different Palindromic Subsequences
题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...
随机推荐
- 在sed中引入shell变量的四种方法
1.eval sed ’s/$a/$b/’ filename2.sed "s/$a/$b/" filename3.sed ’s/’$a’/’$b’/’ filename 4.sed ...
- Cookie实战案例代码
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.s ...
- BFC是什么?有什么作用?
BFC(Block Formatting Context)直译为“块级格式化范围”. 一.常见定位方案 在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案: ...
- tensorflow分布式运行
1.知识点 """ 单机多卡:一台服务器上多台设备(GPU) 参数服务器:更新参数,保存参数 工作服务器:主要功能是去计算 更新参数的模式: 1.同步模型更新 2.异步模 ...
- Hibernate查询总的记录数
1. 原生sql String hql="select count(*) from product" ;//此处的product是数据库中的表名 Query query=sessi ...
- Ironic 裸金属管理服务
目录 文章目录 目录 Ironic 软件架构设计 资源模型设计 全生命周期的状态机设计 Inspection 裸金属上架自检阶段 Provision 裸金属部署阶段 Clean 裸金属回收阶段 快速体 ...
- 六十:Flask.Cookie之flask设置cookie的有效域名
设置cookie有效域名cookie默认只能在主域名下使用,如果要在子域名下使用,则应该给set_cookie设置属性domain='.主域名',此时,此cookie在此主域名下的所有子域名均有效 f ...
- centos 7 删除 virbr0 虚拟网卡
出现虚拟网卡是因为安装时启用了 libvirtd 服务后生成的关闭方法virsh net-list名称 状态 自动开始 持久------------------- ...
- split切割.号的字符串
excel中的日期为下图所示,利用io读取到后,调试发现值为“12.10.2019”,需要将其转换为“2019-10-12” 用split方法以.号切割时,需要用转移字符“\\.”,代码如下 pack ...
- 如何为根分区扩容(centos7为例)
linux系统所有的文件都是存放在根分区中的,如果根分区容量即将耗尽,我们就需要给根分区扩容,我们可以使用lsblk命令来查看,系统的根分区实际是逻辑卷,所以想要扩展根分区只要将逻辑卷扩容就可以了.此 ...