본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 832. Flipping an Image

반응형

https://leetcode.com/problems/flipping-an-image/description/

 

Flipping an Image - LeetCode

Can you solve this real interview question? Flipping an Image - Given an n x n binary matrix image, 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. * F

leetcode.com

구현 문제였습니다.

📕 풀이방법

📔 풀이과정

요구사항 그대로 구현해줍니다.1. 행을 수평으로 뒤집어줍니다.2. images의 각 원소를 flip해줍니다.

📔 정답 출력 | 반환

image를 반환합니다.


📕 Code

📔 C++

class Solution {
public:
    void reverseHorizontally(vector<vector<int>> &image){
        for(auto &im : image) {
            reverse(im.begin(),im.end());
        }
    }

    void flipHorizontally(vector<vector<int>> &image) {
        for(auto &im : image) {
            for(auto &i : im) {
                i = !i;
            }
        }
    }
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& image) {
        reverseHorizontally(image);
        flipHorizontally(image);
        return image;
    }
};

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.