Skip to content

Practice Spiral Matrix II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#59
LeetCode Problem

Spiral Matrix II

Problem Statement

Given a positive integer n, return an n by n matrix containing the integers from 1 through n^2 in clockwise spiral order, starting at the top-left cell and initially moving right.

Source: Spiral Matrix II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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

Related LeetCode Problems

Practice more problems similar to Spiral Matrix II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.