Algorithm


Problem Name: 832. 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.

  • 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: image = [[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: image = [[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]]

 

Constraints:

  • n == image.length
  • n == image[i].length
  • 1 <= n <= 20
  • images[i][j] is either 0 or 1.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector < vector<int>>& A) {
        for(auto& x: A){
            reverse(x.begin(), x.end());
            for(auto& y: x) y ^= 1;
        }
        return A;
    }
}; 
Copy The Code & Try With Live Editor

Input

x
+
cmd
image = [[1,1,0],[1,0,1],[0,0,0]]

Output

x
+
cmd
[[1,0,0],[0,1,0],[1,1,1]]

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int[][] flipAndInvertImage(int[][] A) {
    int numRows = A.length;
    int numCols = A[0].length - 1;
    for (int rowIdx = 0; rowIdx  <  numRows; rowIdx++) {
      int startIdx = 0;
      int endIdx = numCols;
      while (startIdx  < = endIdx) {
        int temp = A[rowIdx][startIdx];
        A[rowIdx][startIdx++] = A[rowIdx][endIdx] == 1 ? 0 : 1;
        A[rowIdx][endIdx--] = temp == 1 ? 0 : 1;
      }
    }
    return A;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
image = [[1,1,0],[1,0,1],[0,0,0]]

Output

x
+
cmd
[[1,0,0],[0,1,0],[1,1,1]]

#3 Code Example with Python Programming

Code - Python Programming


class Solution(object):
    def flipAndInvertImage(self, A):
        return [[1 - x for x in A[i][::-1]] for i in range(len(A))]
Copy The Code & Try With Live Editor

Input

x
+
cmd
image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]

Output

x
+
cmd
[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

#4 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0832_FlippingAnImage
    {
        public int[][] FlipAndInvertImage(int[][] A)
        {
            var row = A.Length;
            var col = A[0].Length;

            for (int i = 0; i  <  row; i++)
            {
                for (int j = 0; j  <  col / 2; j++)
                {
                    var left = A[i][j];
                    var right = A[i][col - j - 1];

                    A[i][j] = right == 1 ? 0 : 1;
                    A[i][col - j - 1] = left == 1 ? 0 : 1;
                }

                if (col % 2 == 1)
                    A[i][col / 2] = A[i][col / 2] == 1 ? 0 : 1;
            }

            return A;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]

Output

x
+
cmd
[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Advertisements

Demonstration


Previous
#831 Leetcode Masking Personal Information Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#833 Leetcode Find And Replace in String Solution in C, C++, Java, JavaScript, Python, C# Leetcode