为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode840. 矩阵中的幻方 | Magic Squares In Grid

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

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

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

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

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.

Given an grid of integers, how many 3 x 3 "magic square" subgrids are there?  (Each subgrid is contiguous).

Example 1:

Input: [[4,3,8,4],
        [9,5,1,9],
        [2,7,6,2]]
Output: 1
Explanation: 
The following subgrid is a 3 x 3 magic square:
438
951
276

while this one is not:
384
519
762

In total, there is only one magic square inside the given grid.

Note:

  1. 1 <= grid.length <= 10
  2. 1 <= grid[0].length <= 10
  3. 0 <= grid[i][j] <= 15

3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。

给定一个由整数组成的 grid,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。

 

示例:

输入: [[4,3,8,4],
      [9,5,1,9],
      [2,7,6,2]]
输出: 1
解释: 
下面的子矩阵是一个 3 x 3 的幻方:
438
951
276

而这一个不是:
384
519
762

总的来说,在本示例所给定的矩阵中只有一个 3 x 3 的幻方子矩阵。

提示:

  1. 1 <= grid.length <= 10
  2. 1 <= grid[0].length <= 10
  3. 0 <= grid[i][j] <= 15

Runtime: 12 ms
Memory Usage: 18.8 MB
 1 class Solution {
 2     func numMagicSquaresInside(_ grid: [[Int]]) -> Int {
 3         var grid = grid
 4         var m:Int = grid.count
 5         if m < 2 {return 0}
 6         var n:Int = grid[0].count
 7         var res:Int = 0
 8         for i in 0..<(m - 2)
 9         {
10             for j in 0..<(n - 2)
11             {
12                 if grid[i + 1][j + 1] == 5 && isValid(&grid, i, j)
13                 {
14                     res += 1
15                 }
16             }            
17         }
18         return res
19     }
20 
21     func isValid(_ grid:inout [[Int]],_ i:Int,_ j:Int) -> Bool
22     {
23         var cnt:[Int] = [Int](repeating:0,count:10)
24         for x in i..<(i + 2)
25         {
26             for y in j..<(j + 2)
27             {
28                 var k:Int = grid[x][y]
29                 if k < 1 || k > 9 || cnt[k] == 1 {return false}
30                 cnt[k] = 1
31             }
32         }
33         if 15 != grid[i][j] + grid[i][j + 1] + grid[i][j + 2] {return false}
34         if 15 != grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2] {return false}
35         if 15 != grid[i][j] + grid[i + 1][j] + grid[i + 2][j] {return false}
36         if 15 != grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1] {return false}
37         if 15 != grid[i][j + 2] + grid[i + 1][j + 2] + grid[i + 2][j + 2] {return false}
38         if 15 != grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2] {return false}
39         if 15 != grid[i + 2][j] + grid[i + 1][j + 1] + grid[i][j + 2] {return false}
40         return true
41     }
42 }

12ms

 1 class Solution {
 2     func numMagicSquaresInside(_ grid: [[Int]]) -> Int {
 3         let R = grid.count, C = grid[0].count;
 4         guard R >= 3 && C >= 3 else {
 5             return 0
 6         }
 7         var ans = 0;
 8         for r in 0..<R-2 {
 9             for c in 0..<C-2 {
10                 if grid[r+1][c+1] != 5 {
11                      continue  // optional skip
12                 }
13                 if magic(vals: grid[r][c], grid[r][c+1], grid[r][c+2],
14                           grid[r+1][c], grid[r+1][c+1], grid[r+1][c+2],
15                           grid[r+2][c], grid[r+2][c+1], grid[r+2][c+2]) {
16                     ans += 1
17                 }
18             }
19         }
20         return ans;
21     }
22     
23     fileprivate func magic(vals: Int...) -> Bool {
24         var count = [Int](repeating: 0, count: 10)
25         for v in vals {
26             guard v >= 0 && v <= 9 else {
27                 return false
28             }
29             count[v] += 1
30         }
31         for v in 1...9 {
32             guard count[v] == 1 else {
33                 return false
34             }
35         }
36         return (vals[0] + vals[1] + vals[2] == 15 &&
37                 vals[3] + vals[4] + vals[5] == 15 &&
38                 vals[6] + vals[7] + vals[8] == 15 &&
39                 vals[0] + vals[3] + vals[6] == 15 &&
40                 vals[1] + vals[4] + vals[7] == 15 &&
41                 vals[2] + vals[5] + vals[8] == 15 &&
42                 vals[0] + vals[4] + vals[8] == 15 &&
43                 vals[2] + vals[4] + vals[6] == 15);
44     }
45 }

16ms

 1 class Solution {
 2     func numMagicSquaresInside(_ grid: [[Int]]) -> Int {
 3         
 4         if grid.count < 3 {
 5             return 0
 6         }
 7         
 8         var result = 0
 9         
10         for r in 0..<grid.count - 2 {
11             
12             let row1 = grid[r]
13             let row2 = grid[r + 1]
14             let row3 = grid[r + 2]
15             
16             for i in 0..<row1.count - 2 {
17                 
18                 if row2[i + 1] != 5 {
19                     continue
20                 }
21                 
22                 if isValid(row1, row2, row3, i) {
23                     result += 1
24                 }
25             }
26         }
27         
28         return result
29     }
30     
31     func isValid(_ row1: [Int], _ row2: [Int], _ row3: [Int], _ start: Int) -> Bool {
32         
33         if row1[start] + row1[start + 1] + row1[start + 2] != 15 { return false }
34         if row2[start] + row2[start + 1] + row2[start + 2] != 15 { return false }
35         if row3[start] + row3[start + 1] + row3[start + 2] != 15 { return false }
36         
37         if row1[start] + row2[start] + row3[start] != 15 { return false }
38         if row1[start + 1] + row2[start + 1] + row3[start + 1] != 15 { return false }
39         if row1[start + 2] + row2[start + 2] + row3[start + 2] != 15 { return false }
40         
41         if row1[start] + row2[start + 1] + row3[start + 2] != 15 { return false }
42         if row1[start + 2] + row2[start + 1] + row3[start] != 15 { return false }
43         
44         var count = Array(repeating: 0, count: 10)
45         for i in start...start + 2 {
46             
47             let v1 = row1[i]
48             let v2 = row2[i]
49             let v3 = row3[i]
50             
51             if v1 > 9 || v2 > 9 || v3 > 9 {
52                 return false
53             }
54             
55             if count[v1] == 1 || count[v2] == 1 || count[v3] == 1 {
56                 return false
57             }
58             
59             count[v1] = 1
60             count[v2] = 1
61             count[v3] = 1
62         }
63         
64         return true
65     }
66 }

20ms

 1 class Solution {
 2   func numMagicSquaresInside(_ grid: [[Int]]) -> Int {
 3     if grid.count < 3 || grid[0].count < 3 {
 4       return 0
 5     }
 6     
 7     var count = 0
 8     
 9     for i in 0..<grid.count - 2 {
10       loopJ: for j in 0..<grid[0].count - 2 {
11         if grid[i + 1][j + 1] != 5 {
12           continue
13         }
14         
15         let v = [grid[i][j], grid[i][j + 1], grid[i][j + 2], grid[i + 1][j], grid[i + 1][j + 1], grid[i + 1][j + 2], grid[i + 2][j], grid[i + 2][j + 1], grid[i + 2][j + 2]]
16         
17         var matches = Array(repeating: 0, count: 16)
18         
19         for num in v {
20           matches[num] = 1
21         }
22         
23         for k in 1...9 {
24           if matches[k] == 0 {
25             continue loopJ
26           }
27         }
28         
29         if v[0] + v[1] + v[2] == 15 && v[3] + v[4] + v[5] == 15 &&
30           v[6] + v[7] + v[8] == 15 && v[0] + v[3] + v[6] == 15 && v[1] + v[4] + v[7] == 15 &&
31           v[2] + v[5] + v[8] == 15 && v[0] + v[4] + v[8] == 15 && v[2] + v[4] + v[6] == 15 {
32           count += 1
33         }
34       }
35     }
36     
37     return count
38   }
39 }

24ms

 1 class Solution {
 2     func numMagicSquaresInside(_ grid: [[Int]]) -> Int {
 3         guard grid.count >= 3 && grid[0].count >= 3 else { return 0 }
 4         var result = 0
 5         for i in 0...grid.count-3 {
 6             for j in 0...grid[0].count-3 {
 7                 let matrix = Array(grid[i..<i+3].map { Array($0[j..<j+3]) })
 8                 if isValid(matrix) {
 9                     result += 1
10                 }
11             }
12         }
13         return result
14     }
15     
16     func isValid(_ matrix: [[Int]]) -> Bool {
17         let allNums = matrix.flatMap { $0 }
18         guard Set(allNums).count == matrix.count * matrix[0].count else { return false }
19         for num in allNums {
20             guard num >= 1 && num <= 9 else { return false }
21         }
22         
23         let rowSums = Set(matrix.map { $0.reduce(0, +) })
24         guard rowSums.count == 1 else { return false }
25         let sum = rowSums.first!
26         
27         for j in matrix[0].indices {
28             if sum != ((0..<matrix.count).map { matrix[$0][j] }.reduce(0, +)) {
29                 return false
30             }
31         }
32         
33         if sum != ((0..<matrix.count).map { matrix[$0][$0] }.reduce(0, +)) {
34             return false
35         }
36 
37         if sum != ((0..<matrix.count).map { (i: Int) in matrix[matrix.count-1-i][i] }.reduce(0, +)) {
38             return false
39         }
40         
41         return true
42     }
43 }

 

posted @ 2019-03-22 17:23  为敢技术  阅读(234)  评论(0编辑  收藏  举报