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. 大约当你拿捏的准世事的分寸时,你便会成功了。(NULL)

    (网络盗图)

  2. Spark应用【根据新df更新旧df】

    // 主键字段保持不变,再转换回来 var columnMap:Map[String, String] = Map() for(key <- keysOpt){ columnMap += (ke ...

  3. SQL Server Replication的分发服务器的快照文件夹位置查找

    SQL Server分发服务器配置中,需要配置快照文件夹(Snapshot Folder),用于存储发布的数据和架构文件的工作目录,那么如何查找当前SQL Server数据库服务器的分发服务器的快照文 ...

  4. 漫说996icu黑名单

    以实际行动声援996icu项目. https://github.com/996icu/996.ICU/blob/master/blacklist/blacklist.md 996公司黑名单,京东,华为 ...

  5. java----JSTL学习笔记(转)

    Java容器类包含List.ArrayList.Vector及map.HashTable.HashMap.Hashset ArrayList和HashMap是异步的,Vector和HashTable是 ...

  6. Linux 安装golang

    访问官方下载地址 或 https://studygolang.com/dl ,32位系统下载go1.9.4.linux-386.tar.gz,64位系统下载go1.9.4.linux-amd64.ta ...

  7. vonic单页面应用

    Vonic—基于Vue.js和ionic样式的移动端UI框架 先放上源码和demo地址: 标签演示:  https://wangdahoo.github.io/vonic/docs/       源码 ...

  8. ORC Creation Best Practices

    Short Description: ORC Creation Best Practices with examples and references. Article Synopsis. ORC i ...

  9. JavaScript在网页中使用以及注意事项

    <script>标签的解析<script>xxx</script>这组标签,是用于在 html 页面中插入 js 的主要方法.它主要有以下几个属性:charset: ...

  10. LDAP安装配置(windows)

    目录 概述 测试环境 安装过程 配置启动 客户端介绍 多级DC的ldif文件的配置 [一].概述 什么叫LDAP呢,概念的东西这里就不多讲了,网上搜索下有很多,本文的重点是介绍如何在windows平台 ...