832. Flipping an Image

 1 public class FlipAndInvertImage {
 2     /**
 3      * 832. Flipping an Image
 4      * Easy
 5      * Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
 6      * To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
 7      * To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].
 8      * Example 1:
 9      * Input: [[1,1,0],[1,0,1],[0,0,0]]
10      * Output: [[1,0,0],[0,1,0],[1,1,1]]
11      * Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
12      * Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
13      * Example 2:
14      * Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
15      * Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
16      * Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
17      * Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
18      * Notes:
19      * 1 <= A.length = A[0].length <= 20
20      * 0 <= A[i][j] <= 1
21      */
22     public int[][] solution(int[][] A) {
23         int aLen = A.length;
24         if (aLen==0)
25             return new int[0][0];
26         int aWei = A[0].length;
27         int[][] res = new int[aLen][aWei];
28         for (int l=0; l<aLen; l++){
29             for (int w=0; w<aWei; w++){
30                 if (A[l][w] == 1)
31                     res[l][aWei-1-w] = 0;
32                 else
33                     res[l][aWei-1-w] = 1;
34             }
35         }
36         return res;
37     }
38 }

 

点赞