Le migliori 150 intervisteFacile

Ruota immagine

Guida dettagliata e implementazione Python per il problema "Ruota immagine".

Dichiarazione del problema

Facile

Ti viene data una matrice 2D n x n che rappresenta un'immagine, ruota l'immagine di 90 gradi (in senso orario).

Devi ruotare l'immagine sul posto, il che significa che devi modificare direttamente la matrice 2D di input. NON allocare un'altra matrice 2D ed eseguire la rotazione.

Implementa una funzione rotate(matrix: list) -> list che ruota la matrice sul posto e la restituisce.

Vincoli
  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

Esempi

Example 1
Input
[[1,2,3],[4,5,6],[7,8,9]]
Output
[[7,4,1],[8,5,2],[9,6,3]]
Explanation

The matrix is rotated 90 degrees clockwise. The first column [1,4,7] becomes the first row [7,4,1] reversed.

Example 2
Input
[[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output
[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Explanation

Each layer of the 4x4 matrix is rotated 90 degrees clockwise.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per matematica e geometria come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.