# LeetCode偶尔一题 —— 832. 翻转图像

## 分析题目

``````/**
* @param {number[][]} A
* @return {number[][]}
*/
var flipAndInvertImage = function(A) {
for (let i = 0; i < A.length; i++) {
let j = 0, k = A[i].length - 1
while (j < k) {
[A[i][j], A[i][k]] = [A[i][k], A[i][j]]
A[i][j] = A[i][j] ? 0 : 1
A[i][k] = A[i][k] ? 0 : 1
j++, k--
}
if (j === k) {
A[i][j] = A[i][j] ? 0 : 1
}
}
return A
};``````

## 优化

``````/**
* @param {number[][]} A
* @return {number[][]}
*/
var flipAndInvertImage = function(A) {
for (let i = 0; i < A.length; i++) {
let j = 0, k = A[i].length - 1
while (j < k) {
[A[i][j], A[i][k]] = [A[i][k], A[i][j]]
A[i][j] ^= 1
A[i][k] ^= 1
j++, k--
}
if (j === k) {
A[i][j] ^= 1
}
}
return A
};``````

## 进阶

``````/**
* @param {number[][]} A
* @return {number[][]}
*/
var flipAndInvertImage = function(A) {
for (let i = 0; i < A.length; i++) {
let j = 0, k = A[i].length - 1
while (j < k) {
if (A[i][j] === A[i][k]) {
A[i][j] ^= 1
A[i][k] ^= 1
}
j++, k--
}
if (j === k) {
A[i][j] ^= 1
}
}
return A
};``````
• 时间复杂度`O(n * k / 2)`
• 空间复杂度`O(1)`