題目大意:將1列與最後n列對換,2列與n-1列對換…而後再將每一個元素取反數組
思路:遍歷二維數組的左半邊,對每一個元素先作對換再取反code
Java實現:ip
public int[][] flipAndInvertImage(int[][] A) { // flip and reverse for (int row=0; row<A.length; row++) { for (int col=0; col<=A[row].length/2; col++) { if (col == A[row].length/2 && A[row].length%2 == 0) break; int end = A[row].length - 1 - col; int tmp = A[row][col]; A[row][col] = invert(A[row][end]); A[row][end] = invert(tmp); System.out.print(A[row][col] + ", "); } System.out.println(); } return A; } int invert(int x) { return x == 1 ? 0 : 1; }