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
 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的更多相关文章

  1. 【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 ...

  2. LeetCode 5272. 5272. 统计参与通信的服务器 Count Servers that Communicate

    地址 https://leetcode-cn.com/problems/count-servers-that-communicate/ 题目描述这里有一幅服务器分布图,服务器的位置标识在 m * n  ...

  3. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  4. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  5. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  6. 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 ...

  7. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  8. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  9. leetcode 730 Count Different Palindromic Subsequences

    题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...

随机推荐

  1. SRS之SrsConfig类

    1. 类定义 1.1 SrsConfig 类 /** * the config service provider. * for the config supports reload, so never ...

  2. setHasFixedSize(true)的意义 (转)

    RecyclerView setHasFixedSize(true)的意义 2017年07月07日 16:23:04 阅读数:6831 <span style="font-size:1 ...

  3. Centos 安装freesurfer fsl matlab 等软件---转自网络

    freesurfer: freesurfer好安装,只需要下载官网源码包,如freesurfer5.3.tar.gz 下载至本地,解压: tar zxvf freesurfer5.3.tar.gz - ...

  4. Cortex-M3 双堆栈指针(MSP&PSP)

    [双堆栈指针(MSP&PSP)] Cortex-M3内核中有两个堆栈指针(MSP & PSP),但任何时刻只能使用到其中一个. 复位后处于线程模式特权级,默认使用MSP. 通过SP访问 ...

  5. 2019-8-13未命名文件 sdfsdfsdfsdfsdfsdf

    2019-8-13未命名文件 sdfsdfsdfsdfsdfsdf 新建模板小书匠 欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过 小书匠主按钮>模板 里的模板管理来改变新建文章 ...

  6. Numpy数据类型转化astype,dtype

    1. 查看数据类型 import numpy as np arr = np.array([1,2,3,4,5]) print(arr) [1 2 3 4 5] # dtype用来查看数据类型 arr. ...

  7. photoshop人物美白教程:暗沉肤色提亮美白

    内容提要:本篇photoshop人物美白教程的制作方法主要通过提取高光选区,配合滤色模式进行人物美白. ps美白教程在网络上搜索,会找到很多.本文介绍的PS人物美白方法,笔者看来是比较好的一种,主要是 ...

  8. JVM配置参数解析

    1.参数说明(部分,待完善) -Xms128M:JVM初始分配的堆内存 -Xmx256M:JVM最大允许分配的堆内存,按需分配 -XX:PermSize=64M: JVM初始分配的非堆内存 -XX:M ...

  9. mariadb数据库集群

    1.主从架构: 每个从节点需要一个dump线程连接主节点 异步:效率高,安全性低,有延迟 同步:效率低,安全性高,无延迟 主:可读可写,(dump thread) 从:可读不可写 (sql threa ...

  10. MessageBox显示位置

    假设存在2个窗口类CImDlg与CChatDlg,如果希望MessageBox跟随CChatDlg,方法是 CChatDlg *pDlg = xxx; pDlg->MessageBox();