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

[Swift]LeetCode661. 图片平滑器 | Image Smoother

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

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

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

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

Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.

Example 1:

Input:
[[1,1,1],
 [1,0,1],
 [1,1,1]]
Output:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0 

Note:

  1. The value in the given matrix is in the range of [0, 255].
  2. The length and width of the given matrix are in the range of [1, 150].

包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们。

示例 1:

输入:
[[1,1,1],
 [1,0,1],
 [1,1,1]]
输出:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]
解释:
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0

注意:

  1. 给定矩阵中的整数范围为 [0, 255]。
  2. 矩阵的长和宽的范围均为 [1, 150]。

456ms

  1 class Solution {
  2     func imageSmoother(_ M: [[Int]]) -> [[Int]] {
  3         if M.count == 0 || M[0].count == 0 {
  4             return M
  5         }
  6         
  7         var result = M
  8         let rowCount = M.count
  9         let columnCount = M[0].count
 10         
 11         for row in 0..<rowCount {
 12             for column in 0..<columnCount {
 13                 let edges = getAllEdges(row: row, column: column, m: M)
 14                 let currVal = M[row][column]
 15                 let sum = currVal + edges.sumOfEdges
 16                 let totalNodes = edges.totalEdges + 1
 17                 let avg: Int = Int(sum/totalNodes)
 18                 result[row][column] = avg
 19             }
 20         }
 21         
 22         return result
 23     }
 24     
 25     func getAllEdges(row: Int, column: Int, m:[[Int]]) -> (sumOfEdges: Int, totalEdges: Int) {
 26         
 27         if m.count == 0 || m[0].count == 0 {
 28             return (sumOfEdges: 0, totalEdges: 0)
 29         }
 30         
 31         var rowCount = m.count
 32         var columnCount = m[0].count
 33         
 34         var result = (sumOfEdges: 0, totalEdges: 0)
 35         
 36         guard row >= 0 && 
 37             row < rowCount && 
 38             column >= 0 &&
 39             column < columnCount else {
 40             return result
 41         }
 42         var edges = 0
 43         var sum = 0
 44         // top Edge
 45         if row != 0 { 
 46             let top = m[row - 1][column]
 47             sum = sum + top
 48             edges = edges + 1
 49         }
 50         
 51         // left
 52         if column != 0 {
 53             let left = m[row][column - 1]
 54             sum = sum + left
 55             edges = edges + 1
 56         }
 57         
 58         // right
 59         if column != columnCount-1 {
 60             let right = m[row][column + 1]
 61             sum = sum + right
 62             edges = edges + 1
 63         }
 64         
 65         //bottom
 66         if row != rowCount-1 {
 67             let bottom = m[row + 1][column]
 68             sum = sum + bottom
 69             edges = edges + 1
 70         }
 71         
 72         //top left
 73         if row != 0 && column != 0 {
 74             let topLeft = m[row-1][column-1]
 75             sum = sum + topLeft
 76             edges = edges + 1
 77         }
 78         
 79         //top right
 80         if row != 0 && column != columnCount-1 {
 81             let topRight = m[row-1][column+1]
 82             sum = sum + topRight
 83             edges = edges + 1
 84         }
 85         
 86         //bottom Left
 87         if row != rowCount-1 && column != 0 {
 88             let bottomLeft = m[row+1][column-1]
 89             sum = sum + bottomLeft
 90             edges = edges + 1
 91         }
 92         
 93         //bottom right
 94         if row != rowCount-1 && column != columnCount-1 {
 95             let bottomRight = m[row+1][column+1]
 96             sum = sum + bottomRight
 97             edges = edges + 1
 98         }
 99         
100         result.sumOfEdges = sum
101         result.totalEdges = edges
102         
103         return result
104     }
105 }

Runtime: 532 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     
 3     let transform = [[-1, -1], [0, -1], [1, -1],
 4                      [-1, 0], [0, 0], [1, 0],
 5                      [-1, 1], [0, 1], [1, 1]]
 6     
 7     func imageSmoother(_ M: [[Int]]) -> [[Int]] {
 8         var N: [[Int]] = []
 9         let row = M.count
10         let colum = M[0].count
11         for i in 0..<row {
12             var array: [Int] = []
13             for j in 0..<colum {
14                 var value = 0
15                 var count = 0
16                 for k in 0..<9 {
17                     let xx = i + transform[k][0]
18                     let yy = j + transform[k][1]
19                     if xx >= 0 && xx < row && yy >= 0 && yy < colum {
20                         value = value + M[xx][yy]
21                         count = count + 1
22                     }
23                 }
24                 array.append(Int(value / count))
25             }
26             N.append(array)
27         }
28         return N
29     }
30 }

Runtime: 544 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func imageSmoother(_ M: [[Int]]) -> [[Int]] {
 3         if M.isEmpty || M[0].isEmpty {return [[]]}
 4         var m:Int = M.count
 5         var n:Int = M[0].count
 6         var res:[[Int]] = M
 7         var dirs:[[Int]] = [[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1]]
 8         
 9         for i in 0..<m
10         {
11             for j in 0..<n
12             {
13                 var cnt:Int = M[i][j]
14                 var all:Int = 1
15                 for dir in dirs
16                 {
17                     var x:Int = i + dir[0]
18                     var y:Int = j + dir[1]
19                     if x < 0 || x >= m || y < 0 || y >= n
20                     {
21                         continue
22                     }
23                     all += 1
24                     cnt += M[x][y]
25                 }
26                 res[i][j] = cnt / all
27             }
28         }
29         return res
30     }
31 }

564ms

 1 class Solution {
 2     func imageSmoother(_ M: [[Int]]) -> [[Int]] {
 3         var M = M
 4         let rows = M.count
 5         let cols = M.first!.count
 6         var retval: [[Int]] = Array(repeating: Array(repeating: 0, count: cols), count: rows)
 7         
 8         for r in 0..<rows {
 9             for c in 0..<cols {
10                 retval[r][c] = ns(&M, r, c)
11             }
12         }
13         
14         return retval
15     }
16     
17     func ns(_ M: inout [[Int]], _ row: Int, _ col: Int) -> Int {
18         let rows = M.count
19         let cols = M.first!.count
20         
21         var count = 0
22         var n = 0
23         if row > 0 {
24             n += (M[row - 1][col])
25             count += 1
26         }
27         if row > 0 && col > 0 {
28             n += (M[row - 1][col - 1])
29             count += 1
30         }
31         if row > 0 && col + 1 < cols {
32             n += (M[row - 1][col + 1])
33             count += 1
34         }
35         if col > 0 {
36             n += (M[row][col - 1])
37             count += 1
38         }
39         if col + 1 < cols {
40             n += (M[row][col + 1])
41             count += 1
42         }
43         if row + 1 < rows {
44             n += (M[row + 1][col])
45             count += 1
46         }
47         if row + 1 < rows && col > 0 {
48             n += (M[row + 1][col - 1])
49             count += 1
50         }
51         if row + 1 < rows && col + 1 < cols {
52             n += (M[row + 1][col + 1])
53             count += 1
54         }
55         n += (M[row][col])
56         count += 1
57         
58         return Int(floor(Double(n)/Double(count)))
59     }
60 }

612ms

 1 class Solution {
 2     func imageSmoother(_ M: [[Int]]) -> [[Int]] {
 3         var result = [[Int]]()
 4         for i in 0..<M.count {
 5             var row = [Int]()
 6             for j in 0..<M[i].count {
 7                 var sum = 0
 8                 var count = 0
 9                 for r in -1...1 {
10                     for c in -1...1 {
11                         if i+r >= 0 && i+r < M.count && j+c >= 0 && j+c < M[0].count {
12                             sum += M[i+r][j+c]
13                             count += 1
14                         }
15                     }
16                 }
17                 row.append(sum/count)
18             }
19             result.append(row)
20         }
21         return result
22     }
23 }

752ms

 1 class Solution {
 2     func imageSmoother(_ M: [[Int]]) -> [[Int]] {
 3         
 4         var ans:[[Int]] = [[Int]](repeating: [Int](repeating: 0, count: M[0].count), count: M.count)
 5         
 6         
 7         for indexX in 0..<M.count {
 8             for indexY in 0..<M[0].count {
 9                 ans[indexX][indexY] = helper(M, indexX, indexY)
10             }
11         }
12         return ans
13     }
14     
15     func helper(_ M: [[Int]],_ indexX:Int, _ indexY:Int) -> Int  {
16         
17             let indexes = [(-1,0), (1,0),
18                        (0,-1),(0,1),
19                         (1,-1),(-1,1),
20                         (1, 1),(-1,-1), (0,0)]
21         
22         var count = 0
23         var total = 0
24         total = 0
25         for index in indexes {
26             let x = indexX + index.0
27             let y = indexY + index.1
28             if x >= 0 && x < M.count && y >= 0 && y < M[0].count {
29                 total += M[x][y]
30                 count += 1
31             }
32         }
33         return total/count
34     }
35 }

996ms

 1 class Solution {
 2   func imageSmoother(_ M: [[Int]]) -> [[Int]] {
 3     var smoothedM = M
 4     
 5     for i in 0..<M.count {
 6       for j in 0..<M[0].count {
 7         let cells = [[i-1, j-1], [i-1, j], [i-1, j+1], [i, j-1], [i, j], [i, j+1], [i+1, j-1], [i+1, j], [i+1, j+1]]
 8         var sum = 0
 9         var count = 0
10         
11         for cell in cells {
12           sum += getSum(M, cell[0], cell[1])
13           count += getCount(M, cell[0], cell[1])
14         }
15         
16         smoothedM[i][j] = sum / count
17       }
18     }
19     
20     return smoothedM
21   }
22   
23   func getSum(_ M: [[Int]], _ i: Int, _ j: Int) -> Int {
24     if i < 0 || j < 0 || i >= M.count || j >= M[0].count {
25       return 0
26     }
27     
28     return M[i][j]
29   }
30   
31   func getCount(_ M: [[Int]], _ i: Int, _ j: Int) -> Int {
32     if i < 0 || j < 0 || i >= M.count || j >= M[0].count {
33       return 0
34     }
35     
36     return 1
37   }
38 }

 

 

posted @ 2019-03-07 12:41  为敢技术  阅读(291)  评论(0编辑  收藏  举报