#832

Flipping an Image

newbie · 140 · lc easy +13 · verified · 83.6% accepted · 3,708 likes · top 95%

Description

Given an n x n binary matrix image, first flip each row horizontally, then invert every bit, and return the resulting matrix.

Flipping a row horizontally reverses the order of its elements.

- For example, flipping [1,1,0] gives [0,1,1].

Inverting replaces each 0 with 1 and each 1 with 0.

- For example, inverting [0,1,1] gives [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]]

Code

1
2
3