Problem Statement
Pattern:
Solution
public int[][] flipAndInvertImage(int[][] A) {
int n = A.length;
for (int[] row : A)
for (int i = 0; i * 2 < n; i++)
if (row[i] == row[n - i - 1])
row[i] = row[n - i - 1] ^= 1;
return A;
}
Notes
row[i] = row[n - i - 1] ^= 1
can be rewritten as,
row[i] = row[n - i - 1] = row[n - i - 1] ^ 1
can be rewritten as, executed in sequence,
row[n - i - 1] = row[n - i - 1] ^ 1
row[i] = row[n - i - 1]
a ^= b equals a = a ^ b
bit manipulation by XOR: https://en.wikipedia.org/wiki/XOR_gate