832. Flipping an Image。

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

原文链接:https://leetcode.com/problems/flipping-an-image/


题目要求将一个矩阵进行变换,先将其的子数组进行逆向,比如[1,1,0],将其逆向为[0,1,1]。然后再将逆向的数组中的所有元素分别进行转换,将1变成0,0变成1,也就是对1进行异或操作。


可以通过观察发现,经过这两次操作之后,对原来的数组来说发生了如下变化:
[1,1,0,0] —> [0,0,1,1] —> [1,1,0,0]
[1,0,0,1] —> [1,0,0,1] —> [0,1,1,0]
[0,1,1,1] —> [1,1,1,0] —> [0,0,0,1]
[1,1,0] —> [0,1,1] —> [1,0,0]
[1,0,1] —> [1,0,1] —> [0,1,0]

可以通过观察发现,由于经过了逆向和与1异或,最初的数组和最终的数组差别:如果前面位置上为0则对称后面位置则设置为1,如果前面为1则对称后面位置上设置为0。后面位置也是这样的规则。

不难写出如下代码:

/*class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {

        if(A.size()==0 || A[0].size()==0)
            return A;

        int p1,p2;
        int i=0,temp;

        while(i < A.size()) {

            p1=0,p2=A[0].size()-1;// 分别从两端向中间进行扫描

            while(p1 <= p2) {
                temp = A[i][p1];// 记录下前面位置上的数字
                A[i][p1] = 1-A[i][p2];//如果对应后面位置为0,则将前面设置为1,否则设置为0
                A[i][p2] = 1-temp;//前面为0,后面设置为1,反之为0.
                p1++;
                p2--;
                if(p1 == p2) {//只剩下中间的一个数字
                    A[i][p1] = 1-A[i][p1];
                    break;
                }
            }
            i++;
        }
        return A;
    }
};*/

再次观察可以发现,如果对称位置上数字不相同的话(一个为0另一个为1),经过变化之后还和原来一样,这种情况下对应位置是不变的。但是如果对称位置上数字相同的话,经过变换之后就相当于与1进行异或操作,所以可以得出如下代码。

class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        for (int i = 0; i < A.size(); ++i){
            int begin = 0;
            int end = A[0].size()-1;
            while (begin < end){
                if(A[i][begin] == A[i][end]){// 如果前面和后面相等,此时需要进行对1进行异或,
                    A[i][begin] ^= 0X1;
                    A[i][end] ^= 0X1;
                }
                ++begin;
                --end;
            }
            if(begin == end) A[i][begin] ^=1;
        }
        return A;
    }
};
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
Here's the code for the pixel_flip function and check_adjacent_for_one helper function: ``` 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. """ if budget == 0: # Base case: no more pixels can be flipped results.append(lst) return if i >= len(lst): # Base case: all pixels have been checked return if lst[i] == 0 and check_adjacent_for_one(orig_lst, i): # Flip the current pixel and recurse with a lower budget new_lst = lst.copy() new_lst[i] = 1 pixel_flip(new_lst, orig_lst, budget - 1, results, i + 1) # Recurse without flipping the current pixel pixel_flip(lst, orig_lst, budget, results, i + 1) 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. """ row_size = int(len(flat_image)**0.5) row = flat_pixel // row_size col = flat_pixel % row_size if row > 0 and flat_image[(row-1)*row_size+col] == 1: return True if row < row_size-1 and flat_image[(row+1)*row_size+col] == 1: return True if col > 0 and flat_image[row*row_size+(col-1)] == 1: return True if col < row_size-1 and flat_image[row*row_size+(col+1)] == 1: return True return False ``` The pixel_flip function takes in the current list being processed, orig_lst which is the original flattened image, budget representing the number of pixels that can still be flipped, results which is initially an empty list of resulting flattened images with flipped pixels, and i representing the index of the pixel being processed. The function 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. It first checks if the budget is 0 or if all pixels have been checked, and returns accordingly. If the current pixel is 0 and has an adjacent pixel with the value of 1, it flips the current pixel and recurses with a lower budget. Otherwise, it recurses without flipping the current pixel. The check_adjacent_for_one helper function takes in the flattened image and the index of the pixel in question, and checks if the pixel has an adjacent pixel with the value of 1. It calculates the row and column of the pixel using integer division and modulus, and checks if the adjacent pixels in the vertical and horizontal directions have the value of 1. If any of the adjacent pixels have the value of 1, it returns True, otherwise it returns False.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值