原地操作,注意列数为奇数的情况:
class Solution {
public int[][] flipAndInvertImage(int[][] A) {
if(A.length==0) return A;
int row = A.length;
int col = A[0].length;
for(int i=0;i<row;i++){
for(int j=0;j<=col/2-1;j++){
int tmp = A[i][j];
A[i][j] = (A[i][col-1-j]==1)?0:1;
A[i][col-1-j] = tmp==1?0:1;
}
}
if(col%2==1){
int col1 = col/2;
for(int i=0;i<row;i++){
A[i][col1] = A[i][col1]==1?0:1;
}
}
return A;
}
}