• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode304.二维区域和检索-矩阵不可变|RangeSumQuery2D-Immutable ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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).

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
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

Note:

  1. You may assume that the matrix does not change.
  2. There are many calls to sumRegion function.
  3. You may assume that row1 ≤ row2 and col1 ≤ col2.

给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。


上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 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
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

说明:

  1. 你可以假设矩阵不可变。
  2. 会多次调用 sumRegion 方法
  3. 你可以假设 row1 ≤ row2 且 col1 ≤ col2。

140ms

 1 class NumMatrix {
 2 
 3     var matrix: [[Int]]
 4     var sumMatrix = [[Int]]()
 5     
 6     init(_ matrix: [[Int]]) {
 7         self.matrix = matrix
 8         sumMatrix = matrix
 9         let m = matrix.count
10         if m == 0 { return }
11         let n = matrix[0].count
12         
13         for i in 0 ..< m { 
14             for j in 1 ..< n {
15                 sumMatrix[i][j] = sumMatrix[i][j - 1] + sumMatrix[i][j]
16             }
17         }
18         for j in 0 ..< n {
19             for i in 1 ..< m {
20                 sumMatrix[i][j] = sumMatrix[i - 1][j] + sumMatrix[i][j]    
21             }
22             
23         }
24     }
25     
26     func sumRegion(_ row1: Int, _ col1: Int, _ row2: Int, _ col2: Int) -> Int {
27         if row1 == 0 && col1 == 0 {
28             return sumMatrix[row2][col2]
29         } else if row1 == 0 {
30             return sumMatrix[row2][col2] - sumMatrix[row2][col1 - 1]
31         } else if col1 == 0 {
32             return sumMatrix[row2][col2] - sumMatrix[row1 - 1][col2]
33         } else {
34             return sumMatrix[row2][col2] - sumMatrix[row1 - 1][col2] - sumMatrix[row2][col1 - 1] + sumMatrix[row1 - 1][col1 - 1] 
35         }
36         
37     }
38 }
39 
40 /**
41  * Your NumMatrix object will be instantiated and called as such:
42  * let obj = NumMatrix(matrix)
43  * let ret_1: Int = obj.sumRegion(row1, col1, row2, col2)
44  */
45  

180ms

 1 class NumMatrix {
 2     
 3     let _matrix : [[Int]]
 4     var sums : [[Int]]
 5     
 6     init(_ matrix: [[Int]]) {
 7         _matrix = matrix
 8         sums = matrix
 9         
10         if matrix.isEmpty {
11             return
12         }
13         
14         for i in 0..<matrix.count {
15             for j in 0..<matrix[0].count {
16                 if i == 0 && j == 0 {
17                     continue
18                 }
19                 if i > 0 && j > 0 {
20                     sums[i][j] += sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1]
21                 }
22                 if i == 0 {
23                     sums[i][j] += sums[i][j-1]
24                 }
25                 
26                 if j == 0 {
27                     sums[i][j] += sums[i-1][j]
28                 }
29             }
30         }
31         
32     }
33     
34     @inline(__always)  func sumRegion(_ row1: Int, _ col1: Int, _ row2: Int, _ col2: Int) -> Int {
35         if row1 == 0 && col1 == 0 {
36             return sums[row2][col2]
37         }
38         
39         if row1 == 0 {
40             return sums[row2][col2] - sums[row2][col1-1]
41         }
42         
43         if col1 == 0 {
44             return sums[row2][col2] - sums[row1-1][col2]
45         }
46         
47         return sums[row2][col2] - sums[row2][col1-1] - sums[row1-1][col2] + sums[row1-1][col1-1]
48     }
49 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift的String与OC的NSString的区别发布时间:2022-07-13
下一篇:
swift实践-06--UITextView发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap