Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Matrices
Bit Manipulation
Flipping an Image
Flip a binary matrix horizontally and then invert every bit.
Acceptance 100%
Problem Statement
Problem
You are given an binary matrix image where each element is either 0 or 1.
Perform two operations in order:
- Flip horizontally each row, so the row is reversed from left to right.
- Invert the matrix, changing every
0to1and every1to0.
Return the transformed matrix.
Notes
- Flipping horizontally means reversing each row independently.
- Inverting means replacing each bit with its opposite value.
Input Format
- An integer matrix
imageof size image[i][j]is either0or1
Output Format
- Return the matrix after horizontally flipping each row and then inverting every value.
Constraints
imageis a square matrix- Each entry is either
0or1
Examples
Sample cases returned by the problem API.
Example 1
Input
image = [[1,1,0],[1,0,1],[0,0,0]]
Output
[[1,0,0],[0,1,0],[1,1,1]]
Explanation
Reverse each row first: [[0,1,1],[1,0,1],[0,0,0]]. Then invert every bit: [[1,0,0],[0,1,0],[1,1,1]].
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.