問題:
難易度:簡単
説明:
int [] []を入力し、配列内のすべての数値をミラーリングしてから、01を反転します。入力要素は01のみです。
件名リンク: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;
}
}