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

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 }

转载于:https://www.cnblogs.com/strengthen/p/10574789.html

[Swift]LeetCode832. 翻转图像 | Flipping an Image相关推荐

  1. C练题笔记之:Leetcode-832. 翻转图像

    题目: 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. 反 ...

  2. Leetcode#832. Flipping an Image(翻转图像)

    题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. ...

  3. 使用Python、OpenCV翻转图像(水平、垂直、水平垂直翻转)

    使用Python.OpenCV翻转图像(水平.垂直.水平垂直翻转) 1. 效果图 2. 源码 参考 这篇博客将介绍如何使用Python.OpenCV翻转图像,类似于cv2.rotate(). 沿y轴水 ...

  4. Leetcode 832. 翻转图像

    832. 翻转图像 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, ...

  5. LeetCode 832. 翻转图像(异或^)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0 ...

  6. LeetCode(832)——翻转图像(JavaScript)

    给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是[0, 1, 1]. 反转图片的意 ...

  7. opencv 图像平移、缩放、旋转、翻转 图像仿射变换

    图像几何变换 图像几何变换从原理上看主要包括两种:基于2x3矩阵的仿射变换(平移.缩放.旋转.翻转).基于3x3矩阵的透视变换. 图像平移 opencv实现图像平移 实现图像平移,我们需要定义下面这样 ...

  8. python翻转图片_832. 翻转图像(python)

    给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. 反转图片的 ...

  9. leetcode_832. 翻转图像

    目录 一.题目内容 二.解题思路 三.代码 一.题目内容 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1 ...

最新文章

  1. 搞定了数学,拿下了代码,没想到在这件事上栽了跟头……
  2. 浅析“字典--NSDirctionary”理论
  3. ionic移动开发流程api
  4. 01-JVM与Java体系结构
  5. Web 2.0与云计算
  6. 手机配置网络代理服务器_两张图简说代理服务器和反向代理服务器
  7. org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
  8. CF1067D Computer Game
  9. 新建UE4 c++类
  10. vmware虚拟机安装,网络配置,与xshell和xftp的连接(图文)
  11. 爬虫实战——爬取大麦网
  12. 鸿蒙系统的理解,我所理解的鸿蒙系统
  13. python读取dxf文件_GitHub - XUIgit/dxfReader: dxf文件解析 用来提取CAD中的dxf文件格式所保存的图像信息...
  14. 职称有哪些意义?如何提升职称?
  15. C语言拼图游戏——Windows下基于EasyX且支持鼠标与键盘操作
  16. Ambari2.7.4 + HDP3.1.4 离线安装(2)
  17. 2021年中国股票市场成交情况、政策调整与股票市场异常波动及政策建议分析[图]
  18. linux中vsc是什么作用,在Linux上开始使用Visual Studio代码(VSC)
  19. Tomcat 幽灵猫任意文件读取漏洞(CVE-2020-1938)
  20. 第1章 网络通信基础

热门文章

  1. 管理者必须具备的四大能力
  2. 脱贫帮扶绩效评价-2020年华数杯C题(含python代码)
  3. 成都市历年职工平均年工资与社保养老保险交费标准
  4. 勤哲excel服务器开发实施管理系统的一点小结
  5. 使用数据挖掘软件Rapidminer进行关联规则分析
  6. 从构建分布式秒杀系统聊聊WebSocket推送通知 1
  7. 计蒜客 -- 常用STL题解
  8. 我命由我不由天:程序员保命4招 + 求生10法则
  9. ERP的五大核心思想
  10. tsp问题动态规划python_用Python解决TSP问题(2)——动态规划算法