832. Flip the image (C++)
1 topic description
Given a binary matrix A, we want to flip the image horizontally first, then invert the image and return the result.
To flip a picture horizontally is to flip each line of the picture, that is, reverse the order. For example, the result of horizontally flipping [1, 1, 0] is [0, 1, 1].
Inverting the picture means that all 0s in the picture are replaced by 1s, and all 1s are replaced by 0s. For example, the result of reversing [0, 1, 1] is [1, 0, 0].
2 Example description
2.1 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 reverse the picture: [[1,0,0] ,[0,1,0],[1,1,1]]
2.2 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 reverse the picture: [[1,1,0,0] ,[0,1,1,0],[0,0,0,1],[1,0,1,0]]
3 Problem solving tips
1 <= A.length = A[0].length <= 20
0 <= A[i][j] <= 1
4 Detailed source code (C++)
class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
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;
}
};