zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1267. 统计参与通信的服务器 | Count Servers that Communicate

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11921965.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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


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

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

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

    示例 1:

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

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

    输入:grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
    输出:4
    解释:第一行的两台服务器互相通信,第三列的两台服务器互相通信,但右下角的服务器无法与其他服务器通信。
     

    提示:

    m == grid.length
    n == grid[i].length
    1 <= m <= 250
    1 <= n <= 250
    grid[i][j] == 0 or 1


    Runtime: 460 ms
    Memory Usage: 21 MB
     1 class Solution {
     2     func countServers(_ grid: [[Int]]) -> Int {
     3         if grid.count == 0 || grid[0].count == 0
     4         {
     5             return 0
     6         }
     7         let numRows:Int = grid.count
     8         let numCols:Int = grid[0].count
     9         var rowCount:[Int] = [Int](repeating: 0, count: numRows)
    10         var colCount:[Int] = [Int](repeating: 0, count: numCols)
    11         var totalServers:Int = 0
    12         for row in 0..<numRows
    13         {
    14             for col in 0..<numCols
    15             {
    16                 if grid[row][col] == 1
    17                 {
    18                     rowCount[row] += 1
    19                     colCount[col] += 1
    20                     totalServers += 1
    21                 }
    22             }
    23         }
    24         for row in 0..<numRows
    25         {
    26             for col in 0..<numCols
    27             {
    28                 if grid[row][col] == 1
    29                 {
    30                     if rowCount[row] == 1 && colCount[col] == 1
    31                     {
    32                         totalServers -= 1
    33                     }
    34                 }
    35             }
    36         }
    37         return totalServers
    38     }
    39 }
  • 相关阅读:
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-softmax多分类-06
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-逻辑回归与交叉熵概述-05
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-多层感知器(神经网络)与激活函数概述-04
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-梯度下降算法概述-03
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-线性回归tf.keras概述-02
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-Tensorflow2.2-cpu/gpu环境安装-01
    深度学习-线性回归基础-02
    艾宾浩斯复习
    maven中设置jdk默认编译版本为1.8
    maven 编译命令
  • 原文地址:https://www.cnblogs.com/strengthen/p/11921965.html
Copyright © 2011-2022 走看看