leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit bae85fca0fc54c49a3dc6e0434567f3e7c057fad
parent 74d9c08327a7e36a7df63018e572ebea58fa0362
Author: Jack Sangdahl <jack@pngu.org>
Date:   Wed, 19 Nov 2025 22:19:04 -0700

832. flipping an image

Diffstat:
Aflipping_an_image.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aflipping_an_image.py | 27+++++++++++++++++++++++++++
2 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/flipping_an_image.c b/flipping_an_image.c @@ -0,0 +1,58 @@ +// 832. Flipping an Image +#include "leetcode.h" + +/* + * 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. to invert an image means that each 0 + * is replaced by 1, and each 1 is replaced by 0. + */ + +int **flipAndInvertImage(int **image, int imageSize, int *imageColSize, + int *returnSize, int **returnColumnSizes) { + int n = imageSize, m = imageColSize[0], tmp = 0; + *returnColumnSizes = (int *)malloc(n * sizeof(int)); + *returnSize = n; + for (int i = 0; i < n; i++) + (*returnColumnSizes)[i] = m; + for (int i = 0; i < n; i++) + for (int j = 0; j < m / 2; j++) { + tmp = image[i][n - 1 - j]; + image[i][n - 1 - j] = image[i][j]; + image[i][j] = tmp; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + image[i][j] = !image[i][j]; + return image; +} + +int main() { + int i1i[3][3] = {{1, 1, 0}, {1, 0, 1}, {0, 0, 0}}, rs1, *rcs1; + int i2i[4][4] = {{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}}, rs2, + *rcs2; + struct two_d_arr *i1 = + two_d_arr_init(ARRAY_SIZE(i1i), ARRAY_SIZE(i1i[0]), i1i); + struct two_d_arr *i2 = + two_d_arr_init(ARRAY_SIZE(i2i), ARRAY_SIZE(i2i[0]), i2i); + int **faii1 = + flipAndInvertImage(i1->arr, i1->row_size, i1->col_size, &rs1, &rcs1); + int **faii2 = + flipAndInvertImage(i2->arr, i2->row_size, i2->col_size, &rs2, &rcs2); + for (int i = 0; i < rs1; i++) { + for (int j = 0; j < rcs1[i]; j++) + printf("%d ", faii1[i][j]); + printf("\n"); + } + printf("\n"); + for (int i = 0; i < rs2; i++) { + for (int j = 0; j < rcs2[i]; j++) + printf("%d ", faii2[i][j]); + printf("\n"); + } + printf("\n"); + free(rcs1); + two_d_arr_free(i1); + free(rcs2); + two_d_arr_free(i2); +} diff --git a/flipping_an_image.py b/flipping_an_image.py @@ -0,0 +1,27 @@ +# 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. to invert an image means that each 0 +is replaced by 1, and each 1 is replaced by 0. +""" + + +class Solution(object): + def flipAndInvertImage(self, image): + """ + :type image: List[List[int]] + :rtype: List[List[int]] + """ + return [[1 ^ i for i in j[::-1]] for j in image] + + +if __name__ == "__main__": + obj = Solution() + print(obj.flipAndInvertImage(image=[[1, 1, 0], [1, 0, 1], [0, 0, 0]])) + print( + obj.flipAndInvertImage( + image=[[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]] + ) + )