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

最新文章

  1. C#小程序飞行棋关卡操作
  2. java 生成和解析二维码
  3. Linux 在 i 节点表中的磁盘地址表中,若一个文件的长度是从磁盘地址表的第 1 块到第 11 块 解析?
  4. BZOJ 1047 理想的正方形
  5. 【BZOJ1007】【HNOI2008】水平可见直线
  6. 学习资料 经典SQL语句大全
  7. Linq使用Group By经验总结
  8. SQL2008安装重启失败
  9. mysql中使用正则表达式时的注意事项
  10. Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败
  11. python12--字符串的比较 函数的默认值的细节 三元表达式 函数对象 名称空间 作用域 列表与字典的推导式 四则运算 函数的嵌套
  12. [JVM-1]Java运行时数据区域
  13. Relation Extraction中SVM分类样例unbalance data问题解决 -松弛变量与惩罚因子
  14. windown 安装配置 mvn不是内部或外部命令
  15. Arthas Alibaba 开源 Java 诊断工具
  16. 9:django 表单
  17. SpringCloud分布式开发理解
  18. centOS7关闭防火墙的命令
  19. linux 实时显示文件的内容
  20. PAT 1043【BST与二叉树】

热门文章

  1. xmapp 404设置
  2. CacheDependency 的使用方法
  3. pam和sasl
  4. jquery validate 多种使用方式
  5. 盘古分词修改支持mono和lucene.net3.03
  6. Android 使用MediaPlayer 播放 视频
  7. vmware下Ubuntu屏幕分辨率设置
  8. Elasticsearch 索引、更新、删除文档
  9. C++/C, Java学习资料
  10. vs附加调试 w3p进程没有名称