832.Flipping an Image

LeetCode第832题 解题思路

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.

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].

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].

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]]
Notes:

1 <= A.length = A[0].length <= 20
0 <= A[i][j] <= 1

对于此题,拿到手后分解成两个步骤,第一步镜像反转,第二步01替换。

镜像反转

涉及到镜像反转时,首先考虑单双问题,通过两重for循环嵌套,实现反转,找到中心轴就要先判断单双问题,然后控制第二层for循环的停止位置。

for(int i=0;i<A.length;i++){
            if(A[i].length %2 ==0){
                for(int j =0;j<A[i].length/2;j++){
                    int change = A[i][j];
                    A[i][j] = A[i][A[i].length-j-1];
                    A[i][A[i].length-j-1] =change;
                    }
            }
            else {
                for(int j =0;j<(A[i].length-1)/2;j++){
                    int change = A[i][j];
                    A[i][j] = A[i][A[i].length-j-1];
                    A[i][A[i].length-j-1] =change;
                }
            }
            
        }

01替换

01替换相比较而言就更加简单,双重for循环+if检查,0换成1,1换成0即可

for(int i=0;i<A.length;i++){
            for(int j=0;j<A[i].length;j++){
                if(A[i][j] ==0)
                    A[i][j] =1;
                else if(A[i][j] ==1)
                    A[i][j] =0;
            }
        }

小结

题目很简单,主要是在思考镜像反转的时候需要多加注意。使用双重for循环是一种和简单但是算法复杂度较高的方法。
Runtime: 1 ms, faster than 89.13% of Java online submissions for Flipping an Image.
Memory Usage: 42.8 MB, less than 10.20% of Java online submissions for Flipping an Image
.
算法时间相比较而言不是很快,需观察最优解

分析最优解

class Solution {
    public int[][] flipAndInvertImage(int[][] A) {
        int C = A[0].length;
        for (int[] row: A)
            for (int i = 0; i < (C + 1) / 2; ++i) {
                int tmp = row[i] ^ 1;
                row[i] = row[C - 1 - i] ^ 1;
                row[C - 1 - i] = tmp;
            }

        return A;
    }
}
  1. 镜像反转可以和01置换同时进行,因为两者没有影响。如果先反转再置换和先置换再反转的结果一样,那么可以同时进行。
  2. 在判断A[i].length时,单双数不需要分开判断,因为单数时对称轴会自己替换自己,也没有任何影响

总结

再进行单双判断时要弄清楚对称轴的影响,尽量少使用for循环嵌套,降低算法复杂度。

Create a function pixel_flip(lst, orig_lst, budget, results, i=0) that uses recursion to generate all possible new unique images from the input orig_lst, following these rules: • The input lst is the current list being processed. Initially, this will be the same as orig_lst which is the original flattened image. • The input budget represents the number of pixels that can still be flipped. When the budget reaches 0, no more pixels can be flipped. • The input results is a list of resulting flattened images with flipped pixels. Initially, this will be an empty list. • The input i represents the index of the pixel being processed, by default set to 0, which is used to drive the recursive function towards its base case (i.e., initially starting from i=0). At termination of the function, the argument results should contain all possibilities of the input orig_lst by only flipping pixels from 0 to 1 under both the budget and the adjacency constraints. fill code at #TODO def pixel_flip(lst: list[int], orig_lst: list[int], budget: int, results: list, i: int = 0) -> None: """ Uses recursion to generate all possibilities of flipped arrays where a pixel was a 0 and there was an adjacent pixel with the value of 1. :param lst: 1D list of integers representing a flattened image . :param orig_lst: 1D list of integers representing the original flattened image. :param budget: Integer representing the number of pixels that can be flipped . :param results: List of 1D lists of integers representing all possibilities of flipped arrays, initially empty. :param i: Integer representing the index of the pixel in question. :return: None. """ #TODO def check_adjacent_for_one(flat_image: list[int], flat_pixel: int) -> bool: """ Checks if a pixel has an adjacent pixel with the value of 1. :param flat_image: 1D list of integers representing a flattened image . :param flat_pixel: Integer representing the index of the pixel in question. :return: Boolean. """ #TODO
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值