LeetCode_832. Flipping an Image_Solution

原题链接

原题中文链接

一、题目描述

二、解题思路

题目所描述的意思是对每个数组先进行取反,并且对数组中的每个元素进行取反转换,所以一共要执行两个操作。

  • 使用reverse函数解决水平翻转的操作;
  • 由于是二进制矩阵,所以使X反转后的结果为 1-X。

三、Solution

C++代码:

class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        size_t len = A.size();  //获得二进制数组的长度
        for (int i = 0; i < A.size(); i++)
        {
            reverse(A[i].begin(),A[i].end());  //执行翻转(逆序)二进制矩阵的操作
            for (int j = 0; j < A[i].size(); j++)
            {
                A[i][j] = 1 - A[i][j];  //执行反转二进制矩阵的操作
            }
        }
        return A;
    }
};

四、个人收获

本题主要考察对数组和二进制的基本理解,同时也让我熟悉了reverse函数的用法。

五、参考资料

二进制逆序(字节反转)

原文地址:https://www.cnblogs.com/OctoptusLian/p/9351503.html

时间: 2024-10-19 07:47:48

LeetCode_832. Flipping an Image_Solution的相关文章

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次操作 每次操作将某个括号反转,问将哪个括号反转能使字

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次操作,每次操作会翻转一个括号,让你翻转最

贪心 Codeforces Round #191 (Div. 2) A. Flipping Game

题目传送门 1 /* 2 贪心:暴力贪心水水 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 const int MAXN = 1e2 + 10; 10 const int INF = 0x3f3f3f3f; 11 int a[MAXN]; 12 13 int main(void) //Codeforces Round #191

zoj zju 2991 Flipping Burned Pancakes

Flipping Burned Pancakes Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As a result, one side of a stack of pancakes is often

[LintCode] Binary Tree Flipping

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return

Flipping elements with WPF

http://yichuanshen.de/blog/2010/11/13/flipping-elements-with-wpf/ Have you already seen ForgottenTime’s new flip animation eye candy? If not, it’s about time! It took me several days to figure out how to do it… My first thought was to find out how to

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