[LeetCode] 832. Flipping an Image_Easy
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
基本思路, 先flip每行, 然后翻转. T: O(m*n) S; O(m*n)
Improve: 可以update in place, for i in range(len(row)//2), 然后将left和right翻转之后对调, 即可, 但是时间复杂度一样 O(m*n)
Code
class Solution:
def flipImage(self, A):
lr, lc, ans = len(A), len(A[0]), A
for i in range(lr):
ans[i] = A[i][::-1]
for i in range(lr):
for j in range(lc):
ans[i][j] = 1- ans[i][j]
return ans
[LeetCode] 832. Flipping an Image_Easy的更多相关文章
- Leetcode#832. Flipping an Image(翻转图像)
题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. ...
- LeetCode 832. 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 ...
- 832. Flipping an Image - LeetCode
Question 832. Flipping an Image Solution 题目大意:将1列与最后n列对换,2列与n-1列对换-然后再将每个元素取反 思路:遍历二维数组的左半边,对每个元素先做对 ...
- 【Leetcode_easy】832. Flipping an Image
problem 832. Flipping an Image solution1: class Solution { public: vector<vector<int>> f ...
- 【LeetCode】832. Flipping an Image 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 翻转 + 异或 直接计算 日期 题目地址:https ...
- [LeetCode&Python] Problem 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- [LeetCode] Card Flipping Game 翻卡片游戏
On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...
- Leetcode 832.翻转图像
1.题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1] ...
随机推荐
- javascript的fn方法(转)
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展j ...
- 报错--"npm audit fix" or "npm audit"
如图: 根据提示输入 npm audit fix --force 如图: 根据提示输入: npm audit
- sencha touch 坑爹的Panel,数据不显示了...
一位同学问我一个问题: sencha touch中xtype创建dataview死活不显示!!版本2.3.1,MVC模式,sencha touch创建目录程序很简单,主界面一个tabPanel,两个分 ...
- 【BZOJ5146】有趣的概率 概率+组合数(微积分)
[BZOJ5146]有趣的概率 Description "可爱的妹子就像有理数一样多,但是我们知道的,你在数轴上随便取一个点取到有理数的概率总是0,"芽衣在床上自顾自的说着这句充满 ...
- vue---进行post和get请求
参考文档: https://www.jb51.net/article/125717.htm 使用axios <script src="https://unpkg.com/axios/d ...
- thinkphp---设置路由
在做一个项目,在项目完成之后,配置一下路由,让URL更容易美观. 下面是具体的配置: Common / Conf / config.php // 路由处理 'URL_HTML_SUFFIX'=> ...
- ABP之创建实体
ABP框架是一个非常庞大的框架,里面的东西有很多,那么如果我需要使用ABP进行项目的开发,具体的使用流程是怎样的呢?接下来将以一个简单的电影票管理“系统”为例子具体的实现一下. 一. 实体的创建 实体 ...
- vue报错/ style-loader: Adds some css to the DOM by adding a <style> tag
1.1.1. vue-cli搭建的项目引入.styl/css文件报错 http://blog.csdn.net/z852064121/article/details/72660327 / styl ...
- IE new Date NaN 问题
var ctime = "2015/1/7 10:35"; ctime = ctime.replace(/-/g,"/"); //为了兼容IE var date ...
- POJ-2329 Nearest number - 2(BFS)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1275 De ...