832. Flipping an Image

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求
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].
这里写图片描述
给出一个01矩阵,将其翻转再取反(0-1,1-0)。

2,题目思路
题目看起来有点复杂,其实是要分别对每一行进行单独的处理即可。翻转只是前后数字的交换,取反只是0-1的变换,总体而言并不难。
参考了discussion之后,有一种思路一样但是更简单的办法,直接使用STL的特性来实现,代码更为的简洁,只是效率上并没有太多的提高。

3,程序源码
1:

class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        int row = A.size();
        if(row == 0)    return A;
        int col = A[0].size();
        for(int i=0;i<row;i++)
            flip_reverse(A[i],col);

        return A;
    }
private:
    void flip_reverse(vector<int>& v, int len)
    {
        for(int i = 0;i<len/2;i++)
            swap(v[i], v[len-i-1]);
        for(int i = 0;i<len;i++)
            v[i] = v[i]==1?0:1;
    }
};

2:

class Solution {
public:
        vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        for (auto & row : A) reverse(row.begin(), row.end());
        for (auto & row : A) for (int & i: row) i^=1;   //按位异或
        return A;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值