LeetCode 832. Flipping an Image
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
题目描述:将一个 n*n 的矩阵进行水平翻转以后再进行图像反转( 0 变 1 , 1 变 0 ),求解此时反转后的矩阵。
题目分析:其实很简单,我们将这个过程分为 2 步,第一步是水平翻转,第二步是图像反转,水平翻转我们可以利用 reverse 函数或者申请一个同样规模的矩阵去水平逆序存储矩阵值,图像反转很容易,通过遍历查找,将 1 替换成 0 , 0 替换成 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的更多相关文章
- 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 ...
- [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 ...
- 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] ...
随机推荐
- Process 0:0:0 (0x1ffc) Worker 0x00000001E580A1A0 appears to be non-yielding on Scheduler 3. Thread creation time: 13153975602106.
现场报错如下: Process 0:0:0 (0x1ffc) Worker 0x00000001E580A1A0 appears to be non-yielding on Scheduler 3. ...
- Lua无法排序的问题(Key需要是连续的)
排序的Key需要是连续的 local x = {[1]={x=6}, [2]={x=5}, [3]={x=7}, [5]={x=2}, [6]={x=8}, [7]={x=5}} ---从小到大排序 ...
- echo 在shell及脚本中显示色彩及闪烁警告效果
在shell脚本编写中,echo用于输出字符串等提示信息,当我们需要格外显示色彩及闪烁效果如下: 一.在执行shell中显示色彩: 语法格式: echo -e "\033[颜色1:颜色2m ...
- 负载均衡(nginx、dubbo、zookeeper)
nginx dubbo zookeeper
- "敏捷革命"读书笔记
最近看可一本书 书名叫<敏捷革命>外国著作中文翻译 本来想自己总结读后感但是本书后面都有本章的总结,所以下面都已摘抄为主,以备之后快速浏览 第一章 世界的运作方式已经打破 规划是有用的,而 ...
- Java数据结构简述
1.数组 概念:一个存储元素的线性集合. 数组声明和创建: dataType[] arrayRefVar = new dataType[arraySize]; 二维数组(多维数组)声明和创建: dat ...
- 一个tomcat部署多个应用实例
安装JDK7sudo apt-get install java7-jdk 安装tomcat7 Tomcat7下载地址http://mirror.bjtu.edu.cn/apache/tomcat/to ...
- git使用命令行拉取远程代码仓库中的分支至本地
1.本地创建文件夹用于存放拉取的代码 2.执行git init初始化文件夹 3.与远程代码仓库建立连接 git remote add origin git@github.com.wuylin/noth ...
- Kafka如何删除topic?
Kafka如何删除topic? 今天为大家带来“Kafka删除topic原理解析”,希望可以帮到那些苦于无法删除topic的朋友们. 前提条件: 在启动broker时候开启删除topic的开关,即在s ...
- Nginx使用教程(八):使用Nginx缓存之Memcached缓存
使用Memcache <br\>Memcache是一个通用的内存缓存系统. 它通常用于加速缓慢的数据访问. NGINXmemcached模块提供各种指令,可以配置为直接访问Memcache ...