832. Flipping an Image

2019-08-16 11:15:23来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

832. Flipping an Image

Posted on 2019-07-26 15:07 玛格汉程序猿 阅读(...) 评论(...) 编辑 收藏
 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 }

 


原文链接:https://www.cnblogs.com/bangpenggao/p/11250449.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java分页查询代码

下一篇:Java emoji持久化mysql