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;
}
};

最新文章

  1. TemplateMethod(模块方法模式)
  2. 还敢说你是程序员?一律师闲着没事写了个app,用户量600万
  3. [ASP.NET MVC 小牛之路]18 - Web API
  4. Ajax提交与传统表单提交的区别说明
  5. sql 优化 链接提示 查询提示 标提示
  6. python_way day19 HTML-day5 (form表单验证,CSRF,cookie,session,缓存)
  7. SPOJ 1487 Query on a tree III(划分树)
  8. 使用JAXP进行sax解析
  9. POJ 3278Catch That Cow
  10. 使用PHP在共享内存中存储数据集
  11. SpringMVC数据绑定
  12. Python 写网络爬虫思路分析
  13. Log4j使用详解
  14. gcc链接,去掉不用的函数和data
  15. Springboot 3.需求携带参数的get请求
  16. Codeforces 1088F(贪心+倍增)
  17. 响应式bootstrap - demo
  18. TeamCity Build 步骤的执行策略
  19. 洛谷P1725--琪露诺(单调队列)
  20. React-引入图片的方法

热门文章

  1. stingstream 类
  2. js对象之间的&quot;继承&quot;的五种方法
  3. Linux 常用命令之df du
  4. 本地oracle可以通过localhost连接,无法通过ip地址连接解决方法,oracle远程连接配置
  5. Dialog对话框的几种方式使用实现
  6. Card 卡片
  7. 如何解决使用 JMeter 时遇到的问题
  8. MySQL 将 字符串 转为 整数
  9. Initializer for conditional binding must have Optional type, not &#39;String&#39;
  10. 认识一下 Kafka