Le migliori 150 intervisteFacile

Matrice a spirale

Guida dettagliata e implementazione Python per il problema della "Matrice a spirale".

Dichiarazione del problema

Facile

Data una matrice m x n, restituisci tutti gli elementi della matrice in ordine a spirale.

Implementa una funzione spiralOrder(matrix: list) -> list che restituisce gli elementi in ordine a spirale.

Vincoli
  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

Esempi

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

Spiral order: right across top [1,2,3], down right side [6,9], left across bottom [8,7], up left side [4], then center [5].

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

Spiral order through a 3x4 matrix.

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.