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

Python3

 class Solution:
def flipAndInvertImage(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
return [[1-i for i in row[::-1]] for row in A] # 0. 过程分析
# 1. 先对A进行反向切片A[::-1]
# 2. 1-i可以达到取反的目的
# 3. 取反也可以使用位异或运算符1^i
# 3.1 1二进制为 0001
# 3.2 0二进制为 0000
# 3.3 1^0为 0001,十进制为1
# 3.4 1^1为 0000,十进制为0
# 3.5 则达到了0变1,1变0取反的目的

Flipping an Image的更多相关文章

  1. Flipping elements with WPF

    http://yichuanshen.de/blog/2010/11/13/flipping-elements-with-wpf/ Have you already seen ForgottenTim ...

  2. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  3. 【OpenMesh】Some basic operations: Flipping and collapsing edges

    这一节中你将学到一些OpenMesh中早已提供的基础操作. 内容包括三角形网格边的翻转以及通过连接邻接的顶点边缘折叠. 三角形网格的翻转(Flipping edges) 考虑到两个邻接面的三角形网格中 ...

  4. Flipping Game(枚举)

    Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codeforces Round #191 (Div. 2)---A. Flipping Game

    Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Flipping Parentheses~Gym 100803G

    Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of t ...

  7. [LeetCode] Flipping an Image 翻转图像

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

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

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

  9. LeetCode 832. Flipping an Image

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

随机推荐

  1. 语音通信中终端上的时延(latency)及减小方法

    时延是语音通信中的一个重要指标,当端到端(end2end)的时延(即one-way-delay,单向时延)低于150Ms时人感觉不到,当端到端的时延超过150Ms且小于450Ms时人能感受到但能忍受不 ...

  2. 前端-JavaScript练习2

    用户输入一个年份,判断这个年是否是闰年. 判断闰年条件: ① 非整百年数除以4,无余为闰,有余不闰: ② 整百年数除以400,无余为闰,有余不闰. 比如: 2000年,整百数年,就要用②公式,除以40 ...

  3. MongoDB分片(Sharding)技术

    分片(sharding)是MongoDB用来将大型集合分割到不同服务器(或者说一个集群)上所采用的方法.尽管分片起源于关系型数据库分区,但MongoDB分片完全又是另一回事. 和MySQL分区方案相比 ...

  4. [UE4]Overlap Event 碰撞事件

    一.对于VR中角色的手模型,一般是在角色中另外添加一个球型碰撞体   二.并且一定要勾选“Generate Overlap Events(触发重叠事件)”选项(默认状态是勾选的) 三.添加开始碰撞事件 ...

  5. Java经典代码片段——使用NIO进行快速的文件拷贝

    public static void fileCopy(File in, File out) throws IOException { FileChannel inChannel = new File ...

  6. Windows 10 +VS2019 编译OpenCV 4.1.0

    准备环境 安装cmake 官网 获取代码 github 下载拓展库opencv_contrib github 生成Sln cmake安装完后桌面会有一个快捷方式,可打开cmakeGUI,打开它. 第一 ...

  7. C语言数据结构基础学习笔记——静态查找表

    查找:在数据集合中寻找满足某种条件的数据元素的过程称为查找. 查找表:用于查找的数据集合称为查找表,一般有以下操作:①查找是否在表中:②查找属性:③进行操作. 查找表又分为: ①静态查找表:只可以进行 ...

  8. WebApp的自动测试工具: Jenkins

    一.下载并安装(msi)https://jenkins.io/download/thank-you-downloading-windows-installer-stable/ 在安装过程这, 需要从p ...

  9. ganglia之web界面介绍

    转自:https://blog.csdn.net/lswnew/article/details/79175555 http://www.51niux.com/?id=83 第一篇文章讲述了gangli ...

  10. EXPDP 时报错ORA-31693,ORA-02354,ORA-01555

    使用数据泵导出数据库大表时报错: ORA-31693: 表数据对象 "**"."**" 无法加载/卸载并且被跳过, 错误如下:ORA-02354: 导出/导入数 ...