[LeetCode] 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
这道题让我们翻转图像,翻转的方法是对于二维数组的每一行,先将所有元素位置翻转一下,然后再按顺序将每个像素值取个反。既然要求这么直接明了,那么就按照其说的一步一步来呗,首先翻转每一行,记得一定要加 ‘&’ 号,不然原数组不会被修改。然后在遍历每个数字,让其或上1,达到取反的目的,当然还是必须要加 ‘&’ 号,最后返回修改后的A数组即可,参见代码如下:
解法一:
class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
for (auto &row : A) reverse(row.begin(), row.end());
for (auto &row : A) {
for (int &num : row) num ^= ;
}
return A;
}
};
上面的方法虽然直接了当,但是毕竟修改了原数组A,再来看一种不修改的方法,这里我们新建一个跟A一样长的二维数组,只不过里面的各行还是空的。然后我们遍历A数组的各行,但在遍历各行上的数字时,我们采用从后往前的遍历顺序,然后对于每个数字取反在加入结果res中,这样直接将翻转和取反同时完成了,参见代码如下:
解法二:
class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
vector<vector<int>> res(A.size());
for (int i = ; i < A.size(); ++i) {
for (int j = (int)A[i].size() - ; j >= ; --j) {
res[i].push_back(!A[i][j]);
}
}
return res;
}
};
参考资料:
https://leetcode.com/problems/flipping-an-image/
https://leetcode.com/problems/flipping-an-image/discuss/130590/C%2B%2BJavaPython-Reverse-and-Toggle
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Flipping an Image 翻转图像的更多相关文章
- Leetcode832.Flipping an Image翻转图像
给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. 反转图片的 ...
- Leetcode#832. Flipping an Image(翻转图像)
题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. ...
- Java实现 LeetCode 832 翻转图像(位运算)
832. 翻转图像 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, ...
- leetcode-832翻转图像
翻转图像 思路: 先对图像进行水平翻转,然后反转图片(对每个像素进行异或操作) 代码: class Solution: def flipAndInvertImage(self, A: List[Lis ...
- [LeetCode] 190. Reverse Bits 翻转二进制位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [Swift]LeetCode832. 翻转图像 | Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- Leetcode 832.翻转图像
1.题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1] ...
- 力扣(LeetCode)832. 翻转图像
给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. 反转图片的 ...
- 力扣832. 翻转图像-C语言实现-简单题
题目 传送门 文本 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, ...
随机推荐
- 一个简单的go语言爬虫
package main import ( "bufio" "fmt" "golang.org/x/net/html/charset" &q ...
- shell利用mysql表项的icmp检测
作者:邓聪聪 利用mysql的表项记录IP地址和对应状态 +----+-----------------+--------+--------+ | id | ip_host | desc | stat ...
- HDU 3966 树链剖分后线段树维护
题意: 一棵树, 操作1.$path(a,b)$之间的点权$+k$ 操作2.单点查询 题解: 树链剖分即可,注意代码细节,双向映射 主要是记录一下板子 #include <string.h> ...
- mysql tp5 find_in_set写法
[['','exp',"FIND_IN_SET(".$data['type'].",place_category)"]]
- js 对象,数组,字符串,相互转换
1:对象转换数组 let obj = {'val1':1, 'val2':2, 'val3':3, 'val4':4}; var arr = [] for (let i in obj) { //取键 ...
- HTML 文本内容居中
简单描述:使用bootstrap 的model弹出框,里边的标题内容是靠左的,想把内容居中. 操作:给标题的class加上"text-center". 另外 我发现,在使用mode ...
- PDF怎么旋转页面,只需几步轻松搞定!
有时候我们下载一个PDF文件里面有页面是旋转的情况,用手机看的时候可以把手机旋转过来看,那么用电脑的时候总不可能也转过来看吧,笔记本是可以的台式的是不行的,这个时候我们就需要把PDF文件中旋转的页面转 ...
- 实践笔记_J2EE_Server_Tomcat_tomcat域名绑定_1_单域名绑定
Tomcat域名绑定(1)单域名绑定 1. 测试环境说明 名称 版本 ...
- Topshelf的使用
一.简介 Topshelf可用于创建和管理Windows服务.其优势在于不需要创建windows服务,创建控制台程序就可以.便于调试. 二.官方地址: 1.官网:http://topshelf-pro ...
- ***远程连接MYSQL提示1130 - Host is not allowed to connect to this MySQL server
如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL serve ...