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 <= 200 <= 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的更多相关文章
- Flipping elements with WPF
http://yichuanshen.de/blog/2010/11/13/flipping-elements-with-wpf/ Have you already seen ForgottenTim ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- 【OpenMesh】Some basic operations: Flipping and collapsing edges
这一节中你将学到一些OpenMesh中早已提供的基础操作. 内容包括三角形网格边的翻转以及通过连接邻接的顶点边缘折叠. 三角形网格的翻转(Flipping edges) 考虑到两个邻接面的三角形网格中 ...
- Flipping Game(枚举)
Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
- Flipping Parentheses~Gym 100803G
Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of t ...
- [LeetCode] Flipping an Image 翻转图像
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- [Swift]LeetCode832. 翻转图像 | Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- LeetCode 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
随机推荐
- 禅知Pro 1.6 前台任意文件读取 | 代码审计
禅知 Pro v1.6 前台任意文件读取 | 代码审计 蝉知专业版是基于蝉知企业门户系统开源版开发,继承了蝉知本身的优秀功能.相对于蝉知开源版增强了商品的属性自定义.属性价格定制.物流跟踪.微信支付. ...
- C语言的split功能
其它高级语言都有字符串的split功能,但C没有系统自带的,只能自己写一个了. void c_split(char *src, const char *separator, int maxlen, c ...
- 好用的treeGrid
jquery easyui 官网:http://www.jeasyui.net/plugins/186.html 下面以学校班级情况,先贴出效果图吧! 数据库设计:红色框中为必须要有的列,右边三个为 ...
- 使用JavaMail创建邮件和发送邮件
参考https://www.cnblogs.com/xdp-gacl/p/4216311.html,写的真好,知识在于分享,备份留着看 一.RFC882文档简单说明 RFC882文档规定了如何编写一封 ...
- NOIP 2018 Day1
Fei2Xue@Lian$Tian! 三道原题qwq真的凉 半年前看到有人发说说,梦见省选打开题目,是Please contact lydsy2012@163.com! 没想到一语成谶 大众分300 ...
- Revit 插件产品架构梳理
一.前言 BIM:Building Information Modeling 建筑信息模型,就是将建筑的相关信息附着于模型中,以管理该建筑在设计.算量.施工.运维全生命周期的情况.创建模 ...
- vue项目中icon图标的完美引入
第一步: 进入阿里矢量图标库并登录 地址:https://www.iconfont.cn 第二步: 选择项目需要的图标添加到库 第三步: 选完之后点击右上角的购物车,打开后点击添加到项目,没有就自己建 ...
- spring boot 拦截异常 统一处理
spring boot 默认情况下会映射到 /error 进行异常处理,提示不友好,需要自定义异常处理,提供友好展示 1.自定义异常类(spring 对于 RuntimeException 异常才会进 ...
- Tomcat版本对照表
导入程序前环境要先配好,环境要想配的正确,版本信息一定要了解. Tomcat版本 6.0 7.0 8.0 8.5 9.0 JDK ≥5.0 ≥6.0 ≥7.0 ≥7.0 ...
- 一行js代码识别Selenium+Webdriver及其应对方案
有不少朋友在开发爬虫的过程中喜欢使用Selenium + Chromedriver,以为这样就能做到不被网站的反爬虫机制发现. 先不说淘宝这种基于用户行为的反爬虫策略,仅仅是一个普通的小网站,使用一行 ...