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. win10 出现0x80072efd错误

    0x80072efd 0x80072efd 是网络问题,windows更新或windows应用商店出现0x80072efd问题,请检查本机代理,是否开着小飞机(Shadowsocks)之类的代理工具. ...

  2. [20180914]oracle 12c 表 full_hash_value如何计算.txt

    [20180914]oracle 12c 表 full_hash_value如何计算.txt --//昨天在12c下看表full_hash_value与11g的full_hash_value不同,不过 ...

  3. mssql sqlserver null数据类型专题

    摘要: 下文将详细讲述sql server NULL(空值)的相关知识,如下所示: 实验环境: sql server 2008 R2 NULL(空值)简介: mssql sqlserver null数 ...

  4. 高通 NXP NFC(PN547PN548) 移植流程 android6.0

    一.驱动部分 首先向NXP 的 fae要android 6.0 bring up的代码,如:NFC_NCIHALx_AR0F.4.3.0_M_NoSE 结构目录如下: 1. 添加驱动文件 高通平台需使 ...

  5. win8.1安装win64_11gR2_database_2of2 【INS-13001]】环境不满足最低要求问题

    1. 如图问题: 2. 修改 database\stage\cvu\cvu_prereq.xml, 添加windows 8.1 <OPERATING_SYSTEM RELEASE="6 ...

  6. AI学习---基于TensorFlow的案例[实现线性回归的训练]

    线性回归原理复习 1)构建模型               |_> y = w1x1 + w2x2 + -- + wnxn + b        2)构造损失函数               | ...

  7. Ubuntu 12.04上安装 MongoDB并运行

    Ubuntu 12.04上安装 MongoDB并运行 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 在Terminal输入 sudo apt-key ...

  8. [Java] SpringMVC工作原理之三:ViewResolver

    一.ViewResolver 根据视图的名称将其解析为 View 类型的视图,如通过 ModelAndView 中的视图名称将其解析成 View,View 是用来渲染页面的,也就是将 Model 填入 ...

  9. (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控

    http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...

  10. A - 畅通工程续 最短路

    某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰. 现在 ...