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:

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

Example 2:

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

Example 3:

  1. Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
  2. Output: 4
  3. 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
  1. class Solution {
  2. public:
  3. int countServers(vector<vector<int>>& grid) {
  4. vector<int> row(grid.size(), ), col(grid[].size(), );
  5. for (int i = ; i < grid.size(); ++i) {
  6. for (int j = ; j < grid[].size(); ++j) {
  7. if (grid[i][j] == ) {
  8. row[i]++;
  9. col[j]++;
  10. }
  11. }
  12. }
  13. int cnt = ;
  14. for (int i = ; i < grid.size(); ++i) {
  15. for (int j = ; j < grid[].size(); ++j) {
  16. if ((grid[i][j] == ) && (row[i] > || col[j] > ))
  17. cnt++;
  18. }
  19. }
  20. return cnt;
  21. }
  22. };

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. GLSL语法入门

    变量 GLSL的变量命名方式与C语言类似.变量的名称可以使用字母,数字以及下划线,但变量名不能以数字开头,还有变量名不能以gl_作为前缀,这个是GLSL保留的前缀,用于GLSL的内部变量.当然还有一些 ...

  2. JS编程规范

    在第一家公司用C++时,公司有着严格的代码规范,甚至到了严苛的地步,现在回想起来,对它充满感激.一个好的习惯让你收益终身. 之后使用JS/TS却没有为自己定一套编程规范,所幸为时不晚,在这里参考air ...

  3. leetcode334 递增的三元子序列

    class Solution { public: bool increasingTriplet(vector<int>& nums) { //使用双指针: int len=nums ...

  4. [django]update_or_create使用场景

    update_or_create 作用是为了添加数据时防止重复. 先去查询, 如果没有在创建, 如果有则更新. update_or_create用法与密码存储实例 create方法 如果id是None ...

  5. ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS)

    ylbtech-ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS) 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. ...

  6. vue-cli3的安装使用

    一.安装vue-cli3 1.全局安装vue-cli 使用命令 cnpm install  -g @vue/cli . npm install  -g @vue/cli.yarn global add ...

  7. golang(06)函数介绍

    原文链接 http://www.limerence2017.com/2019/09/11/golang11/#more 函数简介 函数是编程语言中不可缺少的部分,在golang这门语言中函数是一等公民 ...

  8. web站点放在nginx其他目录下

    web站点放在nginx其他目录下 .查看主配置文件 [root@bogon mysql]# cat /etc/nginx/nginx.conf user root root; worker_proc ...

  9. re&xpath&bs4

    一.re 二.xpath 三.bs4

  10. 一步一步搭建:spark之Standalone模式+zookeeper之HA机制

    理论参考:http://www.cnblogs.com/hseagle/p/3673147.html 基于3台主机搭建:以下仅是操作步骤,原理网上自查 :1. 增加ip和hostname的对应关系,跨 ...