题目描述

给定一个二进制矩阵 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(翻转图像)的更多相关文章

  1. [LeetCode] Flipping an Image 翻转图像

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  2. LeetCode 832. Flipping an Image

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  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. Leetcode832.Flipping an Image翻转图像

    给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. 反转图片的 ...

  5. LeetCode 832 Flipping an Image 解题报告

    题目要求 Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the ...

  6. Java实现 LeetCode 832 翻转图像(位运算)

    832. 翻转图像 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, ...

  7. 832. Flipping an Image - LeetCode

    Question 832. Flipping an Image Solution 题目大意:将1列与最后n列对换,2列与n-1列对换-然后再将每个元素取反 思路:遍历二维数组的左半边,对每个元素先做对 ...

  8. leetcode-832翻转图像

    翻转图像 思路: 先对图像进行水平翻转,然后反转图片(对每个像素进行异或操作) 代码: class Solution: def flipAndInvertImage(self, A: List[Lis ...

  9. 【Leetcode_easy】832. Flipping an Image

    problem 832. Flipping an Image solution1: class Solution { public: vector<vector<int>> f ...

随机推荐

  1. spring整合redis连接

    两种连接方式:(写了一半,未测试) spring xml: <?xml version="1.0" encoding="UTF-8"?> <b ...

  2. 工具类:Colletions ,Arrays(静态导入,可变参数,强循环)

    一.Collecti 专门用来操作集合的工具类,没有构造函数,全静态方法. 常用方法: static <T extends Comparable<? super T>> voi ...

  3. Mock4 moco框架中如何加入cookies

    新建一个 startupWithCookies.json,因为cookies也是请求当中带的,所以,要写在request里面,cookies是k-v的形式,就拿登陆来说吧,登陆以后会的cookies, ...

  4. Druid 数据库连接池

    druid 数据库连接池 由阿里提供 步骤 1 导包 durid1.0.9 jar 包 2 定义配置文件 必须是 properties文件 名字任意 位置也任意 3 获得数据库连接池对象 通过 Dur ...

  5. 7.Django

    1.遍历数据 2.正则表达式匹配数字 ##url超链接 ##配置url ##POST请求需要设置csrf_token

  6. 浏览器console打印定义样式

    %指令 c:表示样式(css) 其他的大家查资料吧 console.log("%c dsajfklsdjljfdskl", "color:red;font-size:50 ...

  7. 2017-12-15python全栈9期第二天第七节之数字转换成布尔值

    #!/user/bin/python# -*- coding:utf-8 -*-print(bool(2))

  8. Ubuntu vim下 实现函数跳转功能

    安装sudo apt-get install exuberant-ctags   在每次使用时,需要初始化tags,只有这样才能使用跳转功能 初始化: 进入项目的顶级目录.输入以下命令.        ...

  9. 编写高质量Python代码总结:待完成

    1:字符串格式化 #避免%过多影响阅读 print('hello %(name)s'%{'name':'tom'}) #format方法print('{name} is very {emmition} ...

  10. expdp和impdp导入导出用法【转】

    关于expdp和impdp exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.expdp和impdp是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用.i ...