leetcode

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

commit 48092c680db162869b6a39e8e372bbb0f1bfb399
parent a68605df316819569152e92b7d727f4ca8e2788f
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sun,  3 May 2026 21:34:38 -0600

48. rotate image

Diffstat:
Arotate_image.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Arotate_image.py | 35+++++++++++++++++++++++++++++++++++
2 files changed, 89 insertions(+), 0 deletions(-)

diff --git a/rotate_image.c b/rotate_image.c @@ -0,0 +1,54 @@ +// 48. Rotate Image +#include "leetcode.h" + +/* + * you are given an 'n x n' 2d 'matrix' representing an image, rotate the image + * by 90 degrees (clockwise). you have to rotate the image in place, meaning you + * have to modify the input 2d matrix directly. do not allocate another 2d + * matrix and do the rotation. + */ + +void rotate(int **matrix, int matrixSize, int *matrixColSize) { + int n = matrixSize; + for (int i = 0; i < n >> 1; i++) + for (int j = i; j < n - 1 - i; j++) { + int tmp = matrix[i][j]; + matrix[i][j] = matrix[~j + n][i]; + matrix[~j + n][i] = matrix[~i + n][~j + n]; + matrix[~i + n][~j + n] = matrix[j][~i + n]; + matrix[j][~i + n] = tmp; + } +} + +int main() { + int m1i[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + int r1[3][3] = {{7, 4, 1}, {8, 5, 2}, {9, 6, 3}}; + int m2i[4][4] = { + {5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}}; + int r2[4][4] = { + {15, 13, 2, 5}, {14, 3, 4, 1}, {12, 6, 8, 9}, {16, 7, 10, 11}}; + struct two_d_arr *m1 = + two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + struct two_d_arr *m2 = + two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + rotate(m1->arr, m1->row_size, m1->col_size); + rotate(m2->arr, m2->row_size, m2->col_size); + for (int i = 0; i < m1->row_size; i++) { + for (int j = 0; j < m1->col_size[i]; j++) { + printf("%d ", m1->arr[i][j]); + assert(m1->arr[i][j] == r1[i][j]); + } + printf("\n"); + } + printf("\n"); + for (int i = 0; i < m2->row_size; i++) { + for (int j = 0; j < m2->col_size[i]; j++) { + printf("%d ", m2->arr[i][j]); + assert(m2->arr[i][j] == r2[i][j]); + } + printf("\n"); + } + printf("\n"); + two_d_arr_free(m1); + two_d_arr_free(m2); +} diff --git a/rotate_image.py b/rotate_image.py @@ -0,0 +1,35 @@ +# 48. Rotate Image +from typing import List + +""" +you are given an 'n x n' 2d 'matrix' representing an image, rotate the image +by 90 degrees (clockwise). you have to rotate the image in place, meaning you +have to modify the input 2d matrix directly. do not allocate another 2d +matrix and do the rotation. +""" + + +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + n = len(matrix) + for i in range(n >> 1): + for j in range(i, n - 1 - i): + matrix[i][j], matrix[j][~i], matrix[~i][~j], matrix[~j][i] = ( + matrix[~j][i], + matrix[i][j], + matrix[j][~i], + matrix[~i][~j], + ) + + +if __name__ == "__main__": + obj = Solution() + m1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + m2 = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]] + obj.rotate(m1) + obj.rotate(m2) + print(m1) + print(m2)