How to solve the problem of flipping Image by leetcode
This article will explain in detail how leetcode solves the problem of flipping images. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
I. Content of the topic
Given a binary matrix A, we want to flip the image horizontally first, then invert the image and return the result.
Flip horizontally is to flip every line of the picture, that is, reverse order. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
Inverting a picture means that all zeros in the picture are replaced by ones, and all ones are replaced by zeros. 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 flip each line: [[0,1,1],[1,0,1],[0,0,0]];
Then invert the picture: [[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 flip each line: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]];
Then invert the picture: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Tip:
1