Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
YouTube Security VideosŠkoda Peaq im Fahrest: DAS hätten wir nicht erwartet! | CHIP(21.09.2026 um 00:00 Uhr)
Sichere ProgrammierungBackups and other lies(20.09.2026 um 23:42 Uhr)
Sichere ProgrammierungOur linter's "safe" autofix would have silently disabled RBAC(20.09.2026 um 23:54 Uhr)
Sichere ProgrammierungTeaching our on-device assistant to say "I don't know"(20.09.2026 um 23:55 Uhr)
Sichere ProgrammierungThe Tracker Is the Spine(21.09.2026 um 00:02 Uhr)
YouTube Security VideosŠkoda Peaq im Fahrest: DAS hätten wir nicht erwartet! | CHIP(21.09.2026 um 00:00 Uhr)
Sichere ProgrammierungBackups and other lies(20.09.2026 um 23:42 Uhr)
Sichere ProgrammierungOur linter's "safe" autofix would have silently disabled RBAC(20.09.2026 um 23:54 Uhr)
Sichere ProgrammierungTeaching our on-device assistant to say "I don't know"(20.09.2026 um 23:55 Uhr)
Sichere ProgrammierungThe Tracker Is the Spine(21.09.2026 um 00:02 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

LeetCode Challenge: 54. Spiral Matrix - JavaScript Solution 🚀

Reagiere als Erste:r — dein Feedback zählt!

Top Interview 150

The Spiral Matrix problem is a common challenge involving matrix traversal. Let’s break down LeetCode 54: Spiral Matrix and implement a solution that works for any m×n matrix.

🚀 Problem Description

Given an m×n matrix, traverse it in spiral order and return the elements in a single array.

💡 Examples

Example 1
Spiral1

Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]  
Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]

Example 2
Spiral

Input: matrix = [[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]

Constraints

  • 1≤m,n≤10
  • −100≤matrix[i][j]≤100

🏆 JavaScript Solution

We solve this problem by defining four boundaries (top, bottom, left, and right) that shrink as we traverse the matrix in spiral order.

Implementation

var spiralOrder = function(matrix) {
    const result = [];
    let top = 0;
    let bottom = matrix.length - 1;
    let left = 0;
    let right = matrix[0].length - 1;

    while (top <= bottom && left <= right) {
        for (let i = left; i <= right; i++) {
            result.push(matrix[top][i]);
        }
        top++;

        for (let i = top; i <= bottom; i++) {
            result.push(matrix[i][right]);
        }
        right--;

        if (top <= bottom) {
            for (let i = right; i >= left; i--) {
                result.push(matrix[bottom][i]);
            }
            bottom--;
        }

        if (left <= right) {
            for (let i = bottom; i >= top; i--) {
                result.push(matrix[i][left]);
            }
            left++;
        }
    }

    return result;
};

🔍 How It Works

  1. Define Boundaries:

    • top: Tracks the topmost row.
    • bottom: Tracks the bottommost row.
    • left: Tracks the leftmost column.
    • right: Tracks the rightmost column.
  2. Traverse in Spiral Order:

    • Left to Right: Traverse the top row, then increment top.
    • Top to Bottom: Traverse the rightmost column, then decrement right.
    • Right to Left: Traverse the bottom row (if it exists), then decrement bottom.
    • Bottom to Top: Traverse the leftmost column (if it exists), then increment left.
  3. Check for Overlap:

    • Ensure the boundaries (top, bottom, left, right) do not overlap before processing each direction.

🔑 Complexity Analysis

  • Time Complexity: O(m⋅n), where m is the number of rows and n is the number of columns.

    • Each element is processed once.
  • Space Complexity: O(1), excluding the space required for the output array.

📋 Dry Run

Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Dry Run
Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]

✨ Pro Tips for Interviews

  1. Clarify Constraints:

    • Ask if the input matrix can be empty.
    • Confirm if non-rectangular matrices are possible (unlikely, but good to clarify).
  2. Discuss Edge Cases:

    • Single-row matrix: [[1, 2, 3]].
    • Single-column matrix: [[1], [2], [3]].
    • Smallest possible matrix: [[1]].
  3. Highlight Efficiency:

    • Explain why the shrinking boundary approach is O(m⋅n).

📚 Learn More

Check out the full explanation and code walkthrough on my previous Dev.to post:
👉 Valid Sudoku - JavaScript Solution

What’s your preferred method to solve this problem? Let’s discuss! 🚀

Study

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten LeetCode Challenge: 54. Spiral Matrix - JavaScript Solution 🚀

Thematisch verwandte Begriffe: LeetCode, Challenge, Spiral, Matrix · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93957 | A vulnerability has been found in olivier-ls PHP-FTS up to 1.1.3. This a…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick