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;
    }
}

原文地址:https://www.cnblogs.com/wupeixuan/p/9574770.html

时间: 2024-08-30 03:54:42

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 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,

[LeetCode] 832. Flipping an Image_Easy

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,

【Leetcode_easy】832. Flipping an Image

problem 832. Flipping an Image 参考1. Leetcode_easy_832. Flipping an Image; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11214856.html

Leetcode 832.翻转图像

1.题目描述 给定一个二进制矩阵 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]] 解释: 首先翻转每一行: [

leetcode——832. 翻转图像

class Solution(object): def flipAndInvertImage(self, A): """ :type A: List[List[int]] :rtype: List[List[int]] """ for i in range(len(A)): A[i]=A[i][::-1] for j in range(len(A[i])): A[i][j]=1-A[i][j] return A 执行用时 :44 ms, 在所有 

[LeetCode&amp;Python] Problem 832. 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,

LeetCode 7 Reverse Integer(翻转整数)

翻译 翻转一个整型数 例1:x = 123, 返回 321 例2:x = -123, 返回 -321 原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? (来自LeetCode官网) Here are some good questions to ask before coding. Bonus poi

翻转图像

一.题目描述 给定一个二进制矩阵 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]]\) 解释: 首先翻转每

opencv 图像平移、缩放、旋转、翻转 图像仿射变换

图像几何变换 图像几何变换从原理上看主要包括两种:基于2x3矩阵的仿射变换(平移.缩放.旋转.翻转).基于3x3矩阵的透视变换. 图像平移 opencv实现图像平移 实现图像平移,我们需要定义下面这样一个矩阵,tx和ty分别是x和y方向上平移的距离: 图像平移利用仿射变换函数 cv.warpAffine() 实现 实验 # 图像平移 import numpy as np import cv2 as cv img = cv.imread('paojie.jpg') rows, cols = img