Leetcode#832. Flipping an Image(翻转图像)
题目描述
给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。
水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。
反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]。
示例 1:
输入: [[1,1,0],[1,0,1],[0,0,0]]
输出: [[1,0,0],[0,1,0],[1,1,1]]
解释: 首先翻转每一行: [[0,1,1],[1,0,1],[0,0,0]];
然后反转图片: [[1,0,0],[0,1,0],[1,1,1]]
示例 2:
输入: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
输出: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
解释: 首先翻转每一行: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]];
然后反转图片: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
说明:
- 1 <= A.length = A[0].length <= 20
- 0 <= A[i][j] <= 1
思路
先把数组逆序,再遍历数组取反
代码实现
package Array;
/**
* 832. Flipping an Image(翻转图像)
* 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。
* 水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。
* 反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]。
*/
public class Solution832 {
public static void main(String[] args) {
Solution832 solution832 = new Solution832();
int[][] A = new int[][]{{1, 1, 0}, {1, 0, 1}, {0, 0, 0}};
solution832.flipAndInvertImage(A);
}
public int[][] flipAndInvertImage(int[][] A) {
int[][] B = new int[A.length][A[0].length];
//水平翻转
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[0].length; j++) {
B[i][j] = A[i][A[0].length - j - 1];
}
}
//反转
for (int i = 0; i < B.length; i++) {
for (int j = 0; j < B[0].length; j++) {
B[i][j] ^= 1;
}
}
return B;
}
}
Leetcode#832. Flipping an Image(翻转图像)的更多相关文章
- [LeetCode] Flipping an Image 翻转图像
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- LeetCode 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- [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 ...
- Leetcode832.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 ...
- Java实现 LeetCode 832 翻转图像(位运算)
832. 翻转图像 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, ...
- 832. Flipping an Image - LeetCode
Question 832. Flipping an Image Solution 题目大意:将1列与最后n列对换,2列与n-1列对换-然后再将每个元素取反 思路:遍历二维数组的左半边,对每个元素先做对 ...
- leetcode-832翻转图像
翻转图像 思路: 先对图像进行水平翻转,然后反转图片(对每个像素进行异或操作) 代码: class Solution: def flipAndInvertImage(self, A: List[Lis ...
- 【Leetcode_easy】832. Flipping an Image
problem 832. Flipping an Image solution1: class Solution { public: vector<vector<int>> f ...
随机推荐
- Flask 应用上下文
应用上下文(application context) 它的字面意思是 应用上下文,但它不是一直存在的,它只是request context 中的一个对 app 的代理(人),所谓local proxy ...
- win32: WM_PAINT 实现双缓冲缓图
相关参考资料: GDI下实现双缓冲 - http://jingyan.baidu.com/article/e73e26c0f8df2424acb6a76e.html <Win32_19>用 ...
- Python模块之time、random、os、sys、序列化、re
Time模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.tim ...
- 使用postman测试dubbo服务层的方法
下面的项目用的是servlet3.0架构 接口(doubbo消费者项目和服务者项目共享的jar项目中): package serviceinvoke; import com.alibaba.dubbo ...
- python config.ini的应用
config.ini文件的结构是以下这样的:结构是"[ ]"之下是一个section,一部分一部分的结构.以下有三个section,分别为section0,section1,sec ...
- JS学习笔记Day1
一.JS概述 1.什么是JS? 是一种基于对象和事件驱动的客户端脚本语言: 运行环境:浏览器(通过浏览器解释执行) 2.JS产生于哪一年,哪个公司,是谁?第一个名字是什么? 1995年,网景公司.布兰 ...
- php项目核心业务(增、删、改、查)(第三篇)
对增删改查数据库的封装 //php对数据库的封装 //Mysql_fetach($sql)函数查询所有的 function Mysql_fetach($sql){ $conn=mysqli_conne ...
- css换行
1. word-break:break-all;只对英文起作用,以字母作为换行依据 2. word-wrap:break-word; 只对英文起作用,以单词作为换行依据 3. white-space: ...
- OpenOCD-JTAG调试
目录 Todo 概述 断点 快速使用 测试led的断点 NAND调试(进阶) OpenOCD 启动OpenOCD OpenOCD命令 OpenOCD烧录程序 GDB GDB命令 使用条件 使用步骤 E ...
- mybatis_异常
1.HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.Binding ...