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