LeetCode 48 — Rotate Image

Tercer problema de Math & Geometry. Rotar matriz n×n 90° in-place. Truco: transponer + invertir filas.

Enunciado

Rota matriz cuadrada 90° en sentido horario, in-place.


Solución — Transponer + reverse de filas

class Solution:
    def rotate(self, matrix):
        n = len(matrix)
        # 1. Transponer (intercambiar matrix[i][j] con matrix[j][i])
        for i in range(n):
            for j in range(i+1, n):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
        # 2. Invertir cada fila
        for row in matrix:
            row.reverse()

Análisis: O(n²) tiempo, O(1) espacio.

Por qué transpose + reverse rows = rotación 90° horaria

Geometría: transponer es reflejar por la diagonal \. Reflejar luego horizontalmente (reverse de filas) = rotación 90° horaria.


Conexiones

Estado

  • Leído
  • Implementado desde cero
  • Resuelto en LeetCode