150 najlepszych wywiadówŁatwe

Matryca spiralna

Szczegółowy przewodnik i implementacja Python dla problemu „Macierz spiralna”.

Oświadczenie o problemie

Łatwe

Biorąc pod uwagę macierz m x n, zwróć wszystkie elementy macierzy w kolejności spiralnej.

Zaimplementuj funkcję spiralOrder(matrix: list) -> list, która zwraca elementy w kolejności spiralnej.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla matematyki i geometrii, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.