问题:
难度:easy
说明:
输入一个 int[][] 然后把数组所有数字镜面翻转,然后 0 1 反转,输入元素只有 0 1。
题目连接:https://leetcode.com/problems/flipping-an-image/
输入案例:
Example 1:
Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
Example 2:
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
我的代码:
class Solution {
private static int[] add = new int[]{1, -1};
public int[][] flipAndInvertImage(int[][] A) {
for(int[] arr : A) {
int len = arr.length - 1, mid = len >> 1;
for(int i = 0;i <= mid;i ++) {
int temp = arr[i], sub = len - i;
arr[i] = arr[sub] + add[arr[sub]];
if(sub != i) arr[sub] = temp + add[temp];
}
}
return A;
}
}