[Swift]LeetCode832. 翻轉圖像 | Flipping an Image

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

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.git

To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].github

To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].微信

Example 1:app

Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

Example 2:ide

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

Notes:spa

  • 1 <= A.length = A[0].length <= 20
  • 0 <= A[i][j] <= 1

給定一個二進制矩陣 A,咱們想先水平翻轉圖像,而後反轉圖像並返回結果。code

水平翻轉圖片就是將圖片的每一行都進行翻轉,即逆序。例如,水平翻轉 [1, 1, 0] 的結果是 [0, 1, 1]htm

反轉圖片的意思是圖片中的 0 所有被 1 替換, 1 所有被 0 替換。例如,反轉 [0, 1, 1] 的結果是 [1, 0, 0]blog

示例 1:

輸入: [[1,1,0],[1,0,1],[0,0,0]]
輸出: [[1,0,0],[0,1,0],[1,1,1]]
解釋: 首先翻轉每一行: [[0,1,1],[1,0,1],[0,0,0]];
     而後反轉圖片: [[1,0,0],[0,1,0],[1,1,1]]

示例 2:

輸入: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
輸出: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
解釋: 首先翻轉每一行: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]];
     而後反轉圖片: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

說明:

  • 1 <= A.length = A[0].length <= 20
  • 0 <= A[i][j] <= 1

Runtime: 32 ms
Memory Usage: 18.7 MB
 1 class Solution {
 2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
 3         var a = Array<Array<Int>>()
 4         for i in 0..<A.count{
 5             var temp = Array<Int>()
 6             for j in 0..<A.count{
 7                 temp.append(1 - A[i][A.count - j - 1])
 8             }
 9             a.append(temp)
10         }
11         return a
12     }
13 }

32ms

1 class Solution {
2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
3         return A.map({$0.reversed().map({$0 == 1 ? 0 : 1})})
4     }
5 }

36ms

 1 class Solution {
 2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
 3         func inverse(_ int: Int) -> Int {
 4             return int == 0 ? 1 : 0
 5         }
 6         return A.map { row in 
 7                       row.compactMap { element in 
 8                                inverse(element)
 9                               }.reversed() 
10         }
11     }
12 }

36ms

 1 class Solution {
 2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
 3         var result: [[Int]] = []
 4         for row in A {
 5             let temp = row.reversed().map{ $0 == 1 ? 0 : 1}
 6             result.append(temp)
 7         }
 8         return result
 9     }
10 }

36ms

 1 class Solution {
 2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
 3         if A.count == 0 || A[0].count == 0{
 4             return A
 5         }
 6 
 7         var A = A
 8         
 9         if A[0].count == 1 {
10             for x in 0..<A.count {
11                 if A[x][0] == 1{
12                     A[x][0] = 0
13                 }else{
14                     A[x][0] = 1
15                 }
16             }
17             return A
18         }
19 
20         let isOdd = A[0].count/2*2 != A[0].count
21         let checkYCount = isOdd ? (A[0].count/2 + 1) : (A[0].count/2)
22         for x in 0..<A.count {
23             for y in 0..<checkYCount {
24                 if A[x][y] == A[x][A[0].count-y-1] {
25                     if A[x][y] == 1{
26                         A[x][y] = 0
27                     }else{
28                         A[x][y] = 1
29                     }
30                     A[x][A[0].count-y-1] = A[x][y]
31                 }
32             }
33         }
34         return A
35     }
36 }

40ms

 1 class Solution {
 2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
 3         if A == nil || A.count == 0 || A[0].count == 0 {
 4             return A
 5         }
 6         let m = A.count, n = A[0].count
 7         var res = [[Int]]()
 8         for array in A {
 9             res.append(array.reversed())
10         }
11         
12         for i in 0 ..< m {
13             for j in 0 ..< n {
14                 if res[i][j] == 0 {
15                     res[i][j] = 1
16                 } else {
17                     res[i][j] = 0
18                 }
19             }
20         }
21         return res
22     }
23 }

52ms

1 class Solution {
2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
3         return A.map {
4             $0.reversed().map { 1 - $0 }
5         }
6     }
7 }

52ms

 1 class Solution {
 2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
 3         var countA = A.count
 4         var res:[[Int]] = [[Int]](repeating:[Int](),count:countA)
 5         for i in 0..<countA
 6         {
 7             for j in stride(from:countA - 1,through:0,by: -1)
 8             {
 9                 res[i].append(1 - A[i][j])
10             }
11         }
12         return res
13     }
14 }

76ms

 1 class Solution {
 2     func flipAndInvertImage(_ A: [[Int]]) -> [[Int]] {
 3         return A.map({ (nums) -> [Int] in
 4             return flip(nums)
 5         })
 6     }
 7 
 8     func flip(_ A: [Int]) -> [Int] {
 9         return A.reversed().map { (num) -> Int in
10             return num == 0 ? 1 : 0
11         }
12     }
13 }
相關文章
相關標籤/搜索