Question

832. Flipping an Image

Solution

题目大意:将1列与最后n列对换,2列与n-1列对换…然后再将每个元素取反

思路:遍历二维数组的左半边,对每个元素先做对换再取反

Java实现:

public int[][] flipAndInvertImage(int[][] A) {
// flip and reverse
for (int row=0; row<A.length; row++) {
for (int col=0; col<=A[row].length/2; col++) {
if (col == A[row].length/2 && A[row].length%2 == 0) break;
int end = A[row].length - 1 - col;
int tmp = A[row][col];
A[row][col] = invert(A[row][end]);
A[row][end] = invert(tmp);
System.out.print(A[row][col] + ", ");
}
System.out.println();
}
return A;
} int invert(int x) {
return x == 1 ? 0 : 1;
}

832. Flipping an Image - LeetCode的更多相关文章

  1. Leetcode#832. Flipping an Image(翻转图像)

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

  2. 【Leetcode_easy】832. Flipping an Image

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

  3. 【LeetCode】832. Flipping an Image 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 翻转 + 异或 直接计算 日期 题目地址:https ...

  4. LeetCode 832. Flipping an Image

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

  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. [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 ...

  7. [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 ...

  8. 832. Flipping an Image —— weekly contest 84

    Flipping an Image Given a binary matrix A, we want to flip the image horizontally, then invert it, a ...

  9. 832. Flipping an Image

    class Solution { public: vector<vector<int>> flipAndInvertImage(vector<vector<int& ...

随机推荐

  1. 面试官:什么是MySQL 事务与 MVCC 原理?

    作者:小林coding 图解计算机基础网站:https://xiaolincoding.com/ 大家好,我是小林. 之前写过一篇 MySQL 的 MVCC 的工作原理,最近有读者在网站上学习的时候, ...

  2. 从CSS盒子模型说起

    前言 总括: 对于盒子模型,BFC,IFC和外边距合并等概念和问题的总结 原文地址:从CSS盒子模型说起 知乎专栏:前端进击者 博主博客地址:Damonare的个人博客 为学之道,莫先于穷理:穷理之要 ...

  3. 左手Cookie“小甜饼”,右手Web Storage

    目录 1. Web Storage 2. Cookie机制 3. 二者的联系与区别 1.Web Storage 1.1 概述 Web Storage是HTML5提供的一种新的浏览器端数据储存机制,它提 ...

  4. 我的python学习记_01

    一切的开始都是从打招呼开始的,python也不例外,首先和将要陪伴我后半生的朋友说句情话: print("不是在最美好的时光遇见你,而是因为遇见你才让我有了最美好的时光") 初写代 ...

  5. python-验证6174猜想

    [题目描述]1955年,卡普耶卡(D.R.Kaprekar)对4位数字进行了研究,发现一个规律:对任意各位数字不相同的4位数,使用各位数字能组成的最大数减去能组成的最小数,对得到的差重复这个操作,最终 ...

  6. python-统计字符个数

    输入一个字符串,统计其中数字字符及小写字符的个数 输入格式: 输入一行字符串 输出格式: 共有?个数字,?个小写字符 输入样例: helo134ss12 输出样例: 共有5个数字,6个小写字符 代码: ...

  7. 一个好用的swagger第三方ui-xiaoymin

    swagger自带的ui界面实在是看的难受 配置完默认访问地址:ip:port/swagger-ui.html 推荐一个好用的第三方ui,界面如图: 使用方法: 1.添加依赖 <dependen ...

  8. java中异常(Exception)的定义,意义和用法。举例

    1.异常(Exception)的定义,意义和用法 我们先给出一个例子,看看异常有什么用? 例:1.1- public class Test {    public static void main(S ...

  9. 【Android开发】Coding + git命令行基本使用

    上传代码 进入本地仓库的目录. cd ... 查看仓库链接 : git remote -v 如果没有,则添加url链接 : git remote add testName https://git.co ...

  10. asp.net core启动源码以及监听,到处理请求响应的过程

    摘要 asp.net core发布至今已经将近6年了,很多人对于这一块还是有些陌生,或者说没接触过:接触过的,对于asp.net core整个启动过程,监听过程,以及请求过程,响应过程也是一知半解,可 ...