Free macOS Software
Activate and Unlock Features

59. Spiral Matrix II

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

Example 1:

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

Example 2:

Input: n = 1
Output: [[1]]

Constraints:

  • 1 <= n <= 20

🔍 Thought Process:

We simulate the spiral traversal of a 2D matrix:

  1. Four directions:
    • ➡️ Right
    • ⬇️ Down
    • ⬅️ Left
    • ⬆️ Up
  2. Boundaries:
    • top, bottom, left, right are used to control the range of movement.
    • After filling one side, move the boundary inward.
  3. Keep a counter from 1 to , and insert values into the correct position as you traverse in spiral order.

✅ Time Complexity:

O(n²) – Every cell in the matrix is filled exactly once.

🔧 Implementations

🔷 Go

func generateMatrix(n int) [][]int {
    result := make([][]int, n)
    for i := range result {
        result[i] = make([]int, n)
    }

    top, bottom := 0, n-1
    left, right := 0, n-1
    num := 1

    for num <= n*n {
        for i := left; i <= right; i++ {
            result[top][i] = num
            num++
        }
        top++
        for i := top; i <= bottom; i++ {
            result[i][right] = num
            num++
        }
        right--
        for i := right; i >= left; i-- {
            result[bottom][i] = num
            num++
        }
        bottom--
        for i := bottom; i >= top; i-- {
            result[i][left] = num
            num++
        }
        left++
    }

    return result
}

🔸 PHP

function generateMatrix($n) {
    $result = array_fill(0, $n, array_fill(0, $n, 0));
    $top = 0;
    $bottom = $n - 1;
    $left = 0;
    $right = $n - 1;
    $num = 1;

    while ($num <= $n * $n) {
        for ($i = $left; $i <= $right; $i++) {
            $result[$top][$i] = $num++;
        }
        $top++;
        for ($i = $top; $i <= $bottom; $i++) {
            $result[$i][$right] = $num++;
        }
        $right--;
        for ($i = $right; $i >= $left; $i--) {
            $result[$bottom][$i] = $num++;
        }
        $bottom--;
        for ($i = $bottom; $i >= $top; $i--) {
            $result[$i][$left] = $num++;
        }
        $left++;
    }

    return $result;
}

🔷 C#

public class Solution {
    public int[][] GenerateMatrix(int n) {
        int[][] matrix = new int[n][];
        for (int i = 0; i < n; i++) {
            matrix[i] = new int[n];
        }

        int top = 0, bottom = n - 1;
        int left = 0, right = n - 1;
        int num = 1;

        while (num <= n * n) {
            for (int i = left; i <= right; i++) {
                matrix[top][i] = num++;
            }
            top++;
            for (int i = top; i <= bottom; i++) {
                matrix[i][right] = num++;
            }
            right--;
            for (int i = right; i >= left; i--) {
                matrix[bottom][i] = num++;
            }
            bottom--;
            for (int i = bottom; i >= top; i--) {
                matrix[i][left] = num++;
            }
            left++;
        }

        return matrix;
    }
}
(0)
Please indicate the source when reproducing:  AIFERE » 59. Spiral Matrix II
Share to

Login

Forgot Password

Sign Up