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

题目描述:将一个 n*n 的矩阵进行水平翻转以后再进行图像反转( 01 , 10 ),求解此时反转后的矩阵。

题目分析:其实很简单,我们将这个过程分为 2 步,第一步是水平翻转,第二步是图像反转,水平翻转我们可以利用 reverse 函数或者申请一个同样规模的矩阵去水平逆序存储矩阵值,图像反转很容易,通过遍历查找,将 1 替换成 00 替换成 1 即可。

python 代码:

class Solution(object):
def flipAndInvertImage(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
A_length = len(A)
A_length_index = len(A[A_length-1])
for i in range(A_length):
A[i].reverse() for i in range(A_length):
for j in range(A_length_index):
if A[i][j] == 0:
A[i][j] = 1
else:
A[i][j] = 0
return A

C++ 代码:

class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
vector<vector<int>> B(A.size());
//vector<int>::reverse_iterator r_iter;
for(int i = 0; i < B.size(); i++){
B[i].resize(A[0].size());
} for(int i = 0; i < A.size(); i++){
int x = 0;
for(int j = A[i].size()-1; j >= 0; j--){
B[i][x] = A[i][j];
x = x + 1;
}
} for(int i = 0; i < B.size(); i++){
for(int j = 0; j < B[i].size(); j++){
if(B[i][j] == 1){
B[i][j] = 0;
}
else{
B[i][j] = 1;
}
}
}
return B;
}
};

LeetCode 832. Flipping an Image的更多相关文章

  1. Leetcode#832. Flipping an Image(翻转图像)

    题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. ...

  2. LeetCode 832 Flipping an Image 解题报告

    题目要求 Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the ...

  3. [LeetCode] 832. Flipping an Image_Easy

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  4. 832. Flipping an Image - LeetCode

    Question 832. Flipping an Image Solution 题目大意:将1列与最后n列对换,2列与n-1列对换-然后再将每个元素取反 思路:遍历二维数组的左半边,对每个元素先做对 ...

  5. 【Leetcode_easy】832. Flipping an Image

    problem 832. Flipping an Image solution1: class Solution { public: vector<vector<int>> f ...

  6. 【LeetCode】832. Flipping an Image 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 翻转 + 异或 直接计算 日期 题目地址:https ...

  7. [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 ...

  8. [LeetCode] Card Flipping Game 翻卡片游戏

    On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...

  9. Leetcode 832.翻转图像

    1.题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1] ...

随机推荐

  1. 关于一体机外卖单不打印外卖单号FAQ(适用正餐6.0.09,轻餐4.0.6.1,轻餐4.0.6.2)

    适用情景:升级版本后打印机打印出的外卖小票不出现外卖单号. 解决方案:设置-功能设置-小票设置-小票自定义-前台小票-外卖订单-------选择编辑,选中右侧中外卖单号或者外卖订单编号,点击保存即可. ...

  2. Centos7安装netstat及简单使用

    Centos7默认不安装netstat组件,需要使用时需要自己安装. 1.查看当前机器net-tools包所在位置 2.安装net-tools包 3.使用netstat命令查看端口占用情况 4.查看指 ...

  3. mysql----Nested SELECT Quiz

     Nested SELECT quiz bbc name region area population gdp Afghanistan South Asia 652225 26000000   Alb ...

  4. Java常用日期操作

    对java中常用的日期操作进行整理. 1.日期格式化 /* * 日期格式化类(必须掌握) * API: * G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Mo ...

  5. Sqlserver还原master

    net stop mssqlserver net start mssqlserver /m"SQLCMD" sqlcmd -s xx sqlcmd -s xx -U sa -P x ...

  6. Sql查询今天、本周和本月的记录(时间字段为时间戳)

    工作中遇到的问题,小结一下 查询今日添加的记录: select * from [表名] where datediff(day,CONVERT(VARCHAR(20),DATEADD(SECOND,[时 ...

  7. ugui中toggle.isOn的属性笔记

    准备知识 toggle:指unity3d引擎中UGUI的 toggle组件 (单选框) 本文使用lua语言描述 事件触发 使用unity的ugui,你如果细心观察会发现toggle在界面被关闭/隐藏( ...

  8. 快速启动神器 Wox

    wox(快速启动程序) wox官网:http://www.wox.one/ 下载wox:https://github.com/Wox-launcher/Wox/releases wox插件库:http ...

  9. 4. svg学习笔记-文档结构元素和样式的使用

    svg除了绘图元素之外还有一部分是专门用于文档结构的,这类元素有<g>,<use>,<defs>,<symbol>等 <g>元素 如果我们仅 ...

  10. PE 添加系统管理员账号(域控可加)转

    使用U盘制作一个PE系统,这里推荐老毛桃或者大白菜:开机进入Bios,选择U盘启动:进入U盘启动画面后,选择一个PE系统:进入PE系统后,我们去本机系统盘,将 C:/Windows/System32/ ...