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. Xcode使用介绍之一:Xcode简介+创建App应用

    一.Xcode简介 Xcode是苹果官方的集成开发环境IDE,开发者可以使用其开发基于iPhone,iPad,Apple Watch以及Mac平台的应用.Xcode具有如下特点: 一站式开发工具.所有 ...

  2. 前端单点登录(SSO)实现方法(二级域名与主域名)

    1.单点登录介绍 单点登录 SSO 全称 Singn Sign On .SSO 是指在多个应用系统中,用户只需要登录一次用户系统,就可以访问其他互相信任的应用系统.例如:在网易官网登录账户,那么再进入 ...

  3. EBI架构 VS. MVC

    和 MVC 模式中的 Model 代表着整个后端(包括所有实体.服务和它们之间的关系在内的一切)一样,EBI 模式将边界看作是和外部世界的完整连接,而不仅仅是一个视图.一个控制器或是一个接口(这里指的 ...

  4. [学习笔记] Tangent Distance

    Tangent Distance 简介 切空间距离可以用在KNN方法中度量距离,其解决的是图像经过有限变换之后还能否被分类正确,例如.对一张数字为5的手写数字图片,将其膨胀后得到图像p1,此时KNN还 ...

  5. AutoItLibrary测试Windows GUI

    AutoItLibrary库关键字 AutoItLibrary 的对象操作大体上有几大主要部分,Window 操作.Control 操作.Mouse 操作.Process操作.Run 操作.Reg 操 ...

  6. 转 Golang 入门 : 切片(slice)

    https://www.jianshu.com/p/354fce23b4f0 切片(slice)是 Golang 中一种比较特殊的数据结构,这种数据结构更便于使用和管理数据集合.切片是围绕动态数组的概 ...

  7. ajax post 请求

    $(".login_btn").click(function(){ if($(".user_").val()=="admin"&&a ...

  8. 在Linux上安装Python3.7.1

    一.安装依赖环境 输入命令:yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readlin ...

  9. pyqt5的QListWidget中设置右键菜单

    QListWidget 是继承 QWidget 的, 所以 QListWidget 是有右键菜单的, 从文档上可以找到 QWidget 上有以下两个与右键菜单有关的函数: Qt.ContextMenu ...

  10. Swift3.0基础语法学习<一>

    // // ViewController.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/15. // Copyright © 2016年 ...