# 832,Flipping an Image

**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
```

方法： 该题比较简单，直接行遍历，然后对于每行只遍历一般即可。 判断： 如果对称位置的数相同，则交换之后是一样的，不管是0还是1，最后都会取反，所以该情况下，两个位置的数都取反。 如果对称位置数不同，则1变成0（0变成1），之后再取反，则为1，因此最后该位置和对称位置的数都不会变化。 最后返回修改后的矩阵。

```
class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        int r=A.size();
        int c=A[0].size();
        for(int i=0;i<r;i++)
        {

            for (int j=0;j<(c+1)/2;j++)
            {
                if(A[i][j] == A[i][c-j-1])

                    A[i][j]  = A[i][c-j-1]=1-A[i][c-j-1];
            }
            }
        return A;
    }
};
```

solution 2:

```
class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        int num=A.size();
        if(!num) return A;
        for(int i=0;i<num;i++)
            for(int j=0;j<(num+1)/2;j++)
            {
                if(A[i][j]==A[i][num-j-1])
                    A[i][j]=A[i][num-j-1] ^=1;
            }
        return A;
        
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/832.flipping_an_image.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
