[Swift]LeetCode832. 翻转图像 | Flipping an Image

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

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

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:

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:

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:

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

给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。

水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]

反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]

示例 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 }

 

上一篇:CSS 3D图片翻转 ——3D Flipping Effect


下一篇:记录英语单词19.03.14