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. scrapy爬虫框架学习笔记(一)

    scrapy爬虫框架学习笔记(一) 1.安装scrapy pip install scrapy 2.新建工程: (1)打开命令行模式 (2)进入要新建工程的目录 (3)运行命令: scrapy sta ...

  2. 基于STM8的TIM定时器操作---STM8-第三章

    1. 综述 STM8S提供三种类型的 TIM 定时器:高级控制型(TIM1).通用型(TIM2/TIM3/TIM5)和基本型定时器(TIM4/TIM6).它们虽有不同功能但都基于共同的架构.此共同的架 ...

  3. phpstorm 如何设置识别vue文件

    非常简单,步骤如下: 第一步: 设置->plugins->browser repositories 搜索vue.js,然后安装一下vue.js插件,重启ide. 第二步: Editor - ...

  4. Axure原型设计工具介绍

    Axure原型设计工具介绍 1759230茅杭斌 目录 1.前言 2.下载与激活 3. Axure相关功能介绍 4.Axure案例演示 5.结语 一.前言 在我们进行程序开发的时候,原型图是必不可少的 ...

  5. SQL 生日得到年龄

    CREATE FUNCTION ufn_hr_getagefrombirthday ( @birthday DATE, @now DATE =NULL ) ) BEGIN IF (@now IS NU ...

  6. redis在实践中的一些常见问题以及优化思路

    1.fork耗时导致高并发请求延时 RDB和AOF的时候,其实会有生成RDB快照,AOF rewrite,耗费磁盘IO的过程,主进程fork子进程 fork的时候,子进程是需要拷贝父进程的空间内存页表 ...

  7. SOD框架的Model、连接数据库及增删改查

    using PWMIS.DataMap.Entity; using System; using System.Collections.Generic; using System.Linq; using ...

  8. this 的指向

    使用 JavaScript 开发的时候,很多开发者多多少少会被 this 的指向搞蒙圈,但是实际上,关于 this 的指向,记住最核心的一句话:哪个对象调用函数,函数里面的this指向哪个对象. 下面 ...

  9. 对象的API

    entries keys values is assign create toSting ProetydefineProperty(obj,key,propety) Object.entries(), ...

  10. leetcode每日刷题计划-简单篇day5

    刷题成习惯以后感觉挺好的 Num 27 移除元素 Remove Element 跟那个排序去掉相同的一样,len标记然后新的不重复的直接放到len class Solution { public: i ...