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 ...
随机推荐
- DAY2---Python---While循环,格式化输出,运算符,编码
一.while循环 while 条件: 代码块(循环体) 流程:判断条件是否为真,如果是真,执行代码块.然后再次判断条件是否为真,如果为真继续执行代码块... 直到条件变成了假,退出循环 #死循环:永 ...
- 苹果电脑利用wget总是会出现无法建立 SSL 连接的问题
在做迁徙学习的过程中,需要下载已经训练好的Inception_v3模型,首先我们为了将下载的模型保存到指定的地方,我们需要利用 wget -P 想要保存的目录 模型的网址,例如 wget -P /Vo ...
- js解析base64
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ...
- 枚举类 enum,结构体类 struct
1.枚举类型的值,直观易于理解,见词知意. 格式: enum 枚举类名:值类型 { 值1, 值2, 值n } 每个值默认(省略“:值类型”)以int型数据存储,从0开始. 使用格式:枚举类名 变量=枚 ...
- qml Loader异步导致ComBoBox数据乱序
qml Loader异步导致ComBoBox数据乱序 当使用Loader动态加载qml文件时, 如果将Loader设置为异步,那么动态加载的组件内的ComBoBox数据将呈现乱序状态, 代码请见下文 ...
- qml: 以鼠标为中心进行放缩;
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true widt ...
- bzoj2938 AC自动机 + 拓扑排序找环
https://www.lydsy.com/JudgeOnline/problem.php?id=2938 题意:给出N个01病毒序列,询问是否存在一个无限长的串不存在病毒序列 正常来说,想要寻找一个 ...
- u-boot(四)命令实现
目录 u-boot(四)命令实现 分析run_command 小结 自定义一个命令 代码 makefile title: u-boot(四)命令实现 tags: linux date: 2018-09 ...
- TF的使用
激活函数 关于激活函数的介绍请参考:激活函数 这里只是记录TF提供的激活函数 import tensorflow as tf a = tf.nn.relu( tf.matmul(x, w1) + ...
- MyBatis-Helloworld
一.依赖配置 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...