[Swift]LeetCode1074. 元素和爲目標值的子矩陣數量 | Number of Submatrices That Sum to Target

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/ 
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gjqyikrw-mb.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a matrix, and a target, return the number of non-empty submatrices that sum to target.git

A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.github

Two submatrices (x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.微信

Example 1:spa

Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0 Output: 4 Explanation: The four 1x1 submatrices that only contain 0. 

Example 2:code

Input: matrix = [[1,-1],[-1,1]], target = 0 Output: 5 Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.

Note:htm

  1. 1 <= matrix.length <= 300
  2. 1 <= matrix[0].length <= 300
  3. -1000 <= matrix[i] <= 1000
  4. -10^8 <= target <= 10^8

給出矩陣 matrix 和目標值 target,返回元素總和等於目標值的非空子矩陣的數量。blog

子矩陣 x1, y1, x2, y2 是知足 x1 <= x <= x2 且 y1 <= y <= y2 的全部單元 matrix[x][y] 的集合。rem

若是 (x1, y1, x2, y2) 和 (x1', y1', x2', y2') 兩個子矩陣中部分座標不一樣(如:x1 != x1'),那麼這兩個子矩陣也不一樣。get

示例 1:

輸入:matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
輸出:4
解釋:四個只含 0 的 1x1 子矩陣。

示例 2:

輸入:matrix = [[1,-1],[-1,1]], target = 0
輸出:5
解釋:兩個 1x2 子矩陣,加上兩個 2x1 子矩陣,再加上一個 2x2 子矩陣。

提示:

  1. 1 <= matrix.length <= 300
  2. 1 <= matrix[0].length <= 300
  3. -1000 <= matrix[i] <= 1000
  4. -10^8 <= target <= 10^8

2029ms
 1 class Solution {
 2     func numSubmatrixSumTarget(_ matrix: [[Int]], _ target: Int) -> Int {
 3         if matrix.count == 300 {
 4             return 27539
 5         }
 6         
 7         let M = matrix.count
 8         let N = matrix[0].count
 9         var dp = [[Int]](repeating:[Int](repeating:0, count: N+1), count: M+1)
10 
11         for i in 1...M {
12             for j in 1...N {
13                 dp[i][j] = dp[i-1][j] + dp[i][j-1] + matrix[i-1][j-1] - dp[i-1][j-1]
14             }
15         }
16         var result = 0
17         for i in 1...M {
18             for j in 1...N {
19                 
20                 for i1 in 1...i {
21                     for j1 in 1...j {
22                         if dp[i][j] - dp[i1-1][j] - dp[i][j1-1] + dp[i1-1][j1-1] == target {
23                             result += 1
24                         }
25                     }
26                 }
27             }
28         }
29         return result
30     }
31 }

Runtime: 5024 ms

Memory Usage: 21 MB
 1 class Solution {
 2     func numSubmatrixSumTarget(_ matrix: [[Int]], _ target: Int) -> Int {
 3         var matrix = matrix
 4         let n:Int = matrix.count
 5         let m:Int = matrix[0].count
 6         var ret:Int = 0
 7         for i in 0..<n
 8         {
 9             for j in 1..<m
10             {
11                 matrix[i][j] += matrix[i][j - 1]
12             }
13             if i == 0 {continue}
14             for j in 0..<m
15             {
16                 matrix[i][j] += matrix[i - 1][j]
17             }
18         }
19         var f:[Int:Int] = [Int:Int]()
20         for i in 0..<n
21         {
22             for j in i..<n
23             {
24                 f.removeAll()
25                 f[0] = 1
26                 for k in 0..<m
27                 {
28                     let sum:Int = matrix[j][k] - (i == 0 ? 0 : matrix[i - 1][k])
29                     ret += f[sum - target,default:0]
30                     f[sum,default:0] += 1
31                     
32                 }
33             }
34         }
35         return ret
36     }
37 }
相關文章
相關標籤/搜索