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, 1, 1].

To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].

Example 1:

Input: [[1,1,0],[1,0,1],[0,0,0]]

Output: [[1,0,0],[0,1,0],[1,1,1]]

Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].

Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

Example 2:

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]

Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].

Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

Notes:

1 <= A.length = A[0].length <= 20

0 <= A[i][j] <= 1

思路:

由题意可知,需要将二维数组中的每一行逆序,然后将二维数组中的全部元素如果是0,则置1,如果是1,则置0。



class Solution {
public:
    void swap(int& a, int& b) {
        int tmp;
        tmp = a;
        a = b;
        b = tmp;
    }

    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        if(A[0].size() > 20) return A;
        int len = A[0].size();
        //int half_len = A[0].size() << 1;
        // flip
        for(int i = 0; i < len; ++i) {
            for(int l = 0, h = len-1; l < h; ++l, --h) {
                if(A[i][l] >= 0 && A[i][l] <= 1
                  && A[i][h] >= 0 && A[i][h] <= 1) {
                    swap(A[i][l], A[i][h]);
                } else {
                    return A;
                }

            }

        }

        // invert
        for(int i = 0; i < len; ++i) {
            for (int j = 0; j < len; ++j) {
                if(A[i][j] == 0) A[i][j] = 1;
                else A[i][j] = 0;
            }
        }
        return A;
    }
};

原文地址:https://www.cnblogs.com/drfxiaoliuzi/p/9203156.html

时间: 2024-10-08 06:14:08

832. Flipping an Image的相关文章

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

【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. 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&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,

CSU - 1542 Flipping Parentheses (线段树)

CSU - 1542 Flipping Parentheses Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Submit Status Description Input Output Sample Input 6 3 ((())) 4 3 1 Sample Output 2 2 1 Hint 题意:先给出一个符合括号匹配的字符串,然后Q次操作 每次操作将某个括号反转,问将哪个括号反转能使字

NYOJ 832 合并游戏

合并游戏 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 大家都知道Yougth除了热爱编程之外,他还有一个爱好就是喜欢玩.某天在河边玩耍的时候,他发现了一种神奇的石子,当把两个石子放在一起的时候,后一个石子会消失,而且会蹦出一定数量的金币,这可乐坏了Yougth,但是他想得到最多的金币,他该怎么做? 输入 首先一行,一个n(1<=n<=10),表示有n个石子.接下来n*n的一个矩阵,Aij表示第i个和第j个合并蹦出的金币值(小于10000,注意合并后j会消失).

codeforces Flipping Game 题解

Iahub got bored, so he invented a game to be played on paper. He writes n integers a1,?a2,?...,?an. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1?≤?i?≤?j?≤?n) and flips all values 

Twitter OA prepare: Flipping a bit

You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one move on the array: choose any two integers [L, R], and flip all the elements between (and including) the L-th and R-th bits. L and R represent the lef

CSU 1542 Flipping Parentheses(线段树)

1542: Flipping Parentheses Time Limit: 5 Sec  Memory Limit: 256 MB Submit: 289  Solved: 79 [Submit][Status][Web Board] Description Input Output Sample Input 6 3 ((())) 4 3 1 Sample Output 2 2 1 HINT 题目大意: 给出一个长度为n的已经匹配的括号字符串,然后有Q次操作,每次操作会翻转一个括号,让你翻转最