[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 <= 200 <= 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] ...
随机推荐
- 【cs229-Lecture3】为什么要选择“最小二乘法”这个指标
视频地址:http://v.163.com/movie/2008/1/E/B/M6SGF6VB4_M6SGHM4EB.html 具体的推导过程,讲义上都有,已经很详细了.这里的推导过程大都是自己为了练 ...
- mysql学习笔记-创建用户以及登录,基本信息查询
第一天: 创建一个mysql账号:create user ‘新的用户名’@‘localhost’identified by ‘口令’: Now()显示当日日期和时间,user()显示当前的用户,ver ...
- mac下编译 boost编译工具b2
cd boost_1_64_0/tools/build ./bootstrap.sh --with-toolset=gcc 输出: -n Bootstrapping the build engine ...
- Android 国内集成使用谷歌地图
extends:http://blog.csdn.net/qduningning/article/details/44778751 由于众做周知的原因在国内使用谷歌地图不太方便,在开发中如果直接使用会 ...
- Docker学习计划三:Dockerfile 使用
我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dockerfile 是镜像和容器的关键,Dockerfile 可以非常容易的定义镜像内容 首先通过一张图来了解 Docker 镜像 ...
- 【转】常用html转义符,JavaScript转义符
HTML字符实体(Character Entities),转义字符串(Escape Sequence) 为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,> ...
- UPUPW本地环境配置thinkphp5的问题
问题解决参考: https://blog.csdn.net/lengyue1084/article/details/80001625 看httpd-vhosts.conf的配置: <Virtua ...
- LCA最近公共祖先(least common ancestors)
#include"stdio.h" #include"string.h" #include"iostream" #include" ...
- Java基础之理解封装,继承,多态三大特性
目录 封装 继承 多态 封装 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 代码理解 publ ...
- HOJ-1005 Fast Food(动态规划)
Fast Food My Tags (Edit) Source : Unknown Time limit : 3 sec Memory limit : 32 M Submitted : 3777, A ...