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
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 ...
随机推荐
- mysql 存储过程的实现原理
一.描述 存储过程是一组可以完成特定功能的SQL语句集,经编译后存储在数据库中 statement语句(DDL.DML.导出及管理语句等).异常处理.流程控制二.创建存储过程 系统做语句分析,如果没有 ...
- [ZZ]MTSC 2017 Mobile Testing@Google 演讲的感受
原文地址: https://testerhome.com/topics/9364 Mobile Testing@Google 其实在开始听谷歌的张南和潘岩开始演讲前,了解下 Google Test C ...
- [转]Linux 基本操作(RM 删除)
来自:http://billie66.github.io/TLCL/book/chap05.html Be Careful With rm! 小心 rm! Unix-like operating sy ...
- centos7安装nginx,以及使用node测试反向代理
1.添加nginx的安装源 vi /etc/yum.repos.d/nginx.repo 2.输入下面内容,并保存退出 [nginx] name=nginx repo baseurl=http://n ...
- 用最简单的话告诉你什么是ElasticSearch
介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 L ...
- mybatis关于ORM的使用以及设计(二)[DaoInterface 转换 Mapper代理对象]
第一节中,分析了Mybatis的ORM框架的初始化,这篇来分析SQL执行过程中,对象->SQL是如何转换的 其中包含两种映射思想 ①DAO接口->Mapper实例 ②执行DAO的方法时,参 ...
- React基础概念
Hello Wrold ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('root') ); ...
- C语言博客作业4--数组
C语言博客作业4--数组 1.本章学习总结 1.1思维导图 请以思维导图总结本周的学习内容,如下图所示: 1.2本章学习体会及代码量学习体会 1.2.1学习体会 描述本周学习感受,也可以在这里提出你不 ...
- 【perl】企业微信发消息
https://open.work.weixin.qq.com/api/doc#90000/90135/90236 #!/usr/bin/env perl use strict; use warnin ...
- ORM版学员管理系统 3
老师信息管理 思考 三种方式创建多对多外键方式及其优缺点. 通过外键创建 class Class(models.Model): id = models.AutoField(primary_key=Tr ...