zoukankan      html  css  js  c++  java
  • [Swift]LeetCode308. 二维区域和检索

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

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

    Range Sum Query 2D
    The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

    Example:

    Given matrix = [
      [3, 0, 1, 4, 2],
      [5, 6, 3, 2, 1],
      [1, 2, 0, 1, 5],
      [4, 1, 0, 1, 7],
      [1, 0, 3, 0, 5]
    ]
    
    sumRegion(2, 1, 4, 3) -> 8
    update(3, 2, 2)
    sumRegion(2, 1, 4, 3) -> 10
    

    Note:

    1. The matrix is only modifiable by the update function.
    2. You may assume the number of calls to update and sumRegion function is distributed evenly.
    3. You may assume that row1 ≤ row2 and col1 ≤ col2.

    给定一个二维矩阵,找到的数目的元素内部定义的rectangle市ITS的左上角(row1, col1)和右下角(row2, col2).

    Range Sum Query 2D

    上面的矩形(带红色边框)由(row1,col1)=(2,1)和(row2,col2)=(4,3)定义,其中包含sum=8。 

    实例:

    给定 matrix = [
      [3, 0, 1, 4, 2],
      [5, 6, 3, 2, 1],
      [1, 2, 0, 1, 5],
      [4, 1, 0, 1, 7],
      [1, 0, 3, 0, 5]
    ]
    
    sumRegion(2, 1, 4, 3) -> 8
    update(3, 2, 2)
    sumRegion(2, 1, 4, 3) -> 10

    注:

    只读modifiable冰矩阵的更新功能。

    你可以要求银行assume'号更新功能和sumregion evenly冰的分布。

    你可能是 row1 ≤ row2 且 col1 ≤ col2.


    Solution:

     1 class NumMatrix {
     2     var mat:[[Int]] = [[Int]]()
     3     var colSum:[[Int]] = [[Int]]()
     4     init(_ matrix:inout [[Int]])
     5     {
     6         if matrix.isEmpty || matrix[0].isEmpty
     7         {
     8             return
     9         }
    10         mat = matrix
    11         colSum = [[Int]](repeating:[Int](repeating:0,count:matrix[0].count),count:matrix.count + 1)
    12         for i in 1..<colSum.count
    13         {
    14             for j in 0..<colSum[0].count
    15             {
    16                 colSum[i][j] = colSum[i - 1][j] + matrix[i - 1][j]
    17             }
    18         }
    19     }
    20     
    21     func update(_ row:Int,_ col:Int,_ val:Int)
    22     {
    23         for i in (row + 1)..<colSum.count
    24         {
    25             colSum[i][col] += val - mat[row][col]
    26         }
    27         mat[row][col] = val
    28     }
    29     
    30     func sumRegion(_ row1:Int,_ col1:Int,_ row2:Int,_ col2:Int) -> Int
    31     {
    32         var res:Int = 0
    33         for j in col1...col2
    34         {
    35             res += colSum[row2 + 1][j] - colSum[row1][j]
    36         }
    37         return res
    38     }
    39 }

    点击:Playground测试

    1 var matrix:[[Int]] = [[3, 0, 1, 4, 2],[5, 6, 3, 2, 1],[1, 2, 0, 1, 5],[4, 1, 0, 1, 7],[1, 0, 3, 0, 5]]
    2 var sol = NumMatrix(&matrix)
    3 print(sol.sumRegion(2, 1, 4, 3))
    4 //Print 8
    5 sol.update(3, 2, 2)
    6 print(sol.sumRegion(2, 1, 4, 3))
    7 //Print 10
  • 相关阅读:
    Java 动态编译
    在ubuntu 18.04下,无线网卡无驱动,连不上wifi,显示wifi没有适配器的解决方法
    由浅入深了解Thrift(1,2,3)
    Docker系列05:docker镜像制作 &Docker file
    Docker系列04:docker数据存储
    Docker系列03:docker网络
    关于在github上 下载源码 clone 非 master 分支的代码
    CentOS 6 & 7 忘记root密码的修改方法
    Windows RDP远程连接CentOS 7
    Windows 上用IntelliJ Idea调试百度大数据分析框架Apache Doris FE
  • 原文地址:https://www.cnblogs.com/strengthen/p/10705800.html
Copyright © 2011-2022 走看看