地址 https://leetcode-cn.com/problems/count-servers-that-communicate/

题目描述
这里有一幅服务器分布图,服务器的位置标识在 m * n 的整数矩阵网格 grid 中,1 表示单元格上有服务器,0 表示没有。

如果两台服务器位于同一行或者同一列,我们就认为它们之间可以进行通信。

请你统计并返回能够与至少一台其他服务器进行通信的服务器的数量。

样例1

输入:grid = [[,],[,]]
输出:
解释:没有一台服务器能与其他服务器进行通信。

样例2

输入:grid = [[,],[,]]
输出:
解释:所有这些服务器都至少可以与一台别的服务器进行通信。

样例3

输入:grid = [[,,,],[,,,],[,,,],[,,,]]
输出:
解释:第一行的两台服务器互相通信,第三列的两台服务器互相通信,但右下角的服务器无法与其他服务器通信。
提示: m == grid.length
n == grid[i].length
<= m <=
<= n <=
grid[i][j] == or

算法1
单独计算同行 同列的元素计数 大于等于2 则算作可互相通讯

class Solution {
public: set<vector<int>> resultSet;
int countServers(vector<vector<int>>& grid) {
for (int i = ; i < grid.size(); i++) {
int firstx = -; int firsty = -;
for (int j = ; j < grid[].size(); j++) {
if (grid[i][j] == ) {
if (firstx == -) {
firstx = i; firsty = j;
}
else {
vector<int>v1{ firstx, firsty };
resultSet.insert(v1);
vector<int>v2{ i, j };
resultSet.insert(v2);
}
}
}
} for (int y = ; y < grid[].size(); y++) {
int firstx = -; int firsty = -;
for (int x = ; x < grid.size(); x++) {
if (grid[x][y] == ) {
if (firstx == -) {
firstx = x; firsty = y;
}
else {
vector<int>v1{ firstx, firsty };
resultSet.insert(v1);
vector<int>v2{ x, y };
resultSet.insert(v2); }
}
}
} return resultSet.size();
} };

最新文章

  1. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别
  2. jquery修改带!important的css样式
  3. CSS样式表(二)
  4. System.Windows.Media.Imageing.BItmapImage 这么用才不会占用文件
  5. Ubuntu 13.04安装搜狗输入法
  6. android——单点触控移动,多点触控放大缩小
  7. python解决汉诺塔问题
  8. Silverlight之OOB模式下的一些事
  9. Androidannotation使用之@Rest与server交互的JSON数据转换(二)
  10. 纯蓝ICON_学习教程
  11. tky项目第①个半月总结
  12. 中国象棋(IOS)
  13. Python3 类和继承和组合
  14. 『实践』VirtualBox 5.1.18+Centos 6.8+hadoop 2.7.3搭建hadoop完全分布式集群及基于HDFS的网盘实现
  15. Office - Excel 2013
  16. P3440 [POI2006]SZK-Schools
  17. redhat6.2 clang编译环境搭建(采用源码包编译安装)
  18. fdisk命令总结
  19. FreeRTOS-03中断测试
  20. JS怎么刷新当前页面

热门文章

  1. SpringCloud之RefreshScope 源码解读
  2. Udp 异步通信(三)
  3. Cannot read property &#39;forEach&#39; of undefined
  4. Unity中的资源管理
  5. Everything可能泄漏大量电脑敏感资料
  6. Asp.Net Core 单元测试正确姿势
  7. Ubuntu 10.04——boa服务器的搭建
  8. 【51nod 1251】 Fox序列的数量(以及带限制插板法讲解)
  9. My Eclipse 配置
  10. npm start 的应用 改为forever 后台启动的方法记录