Skip to main content

Clockwise Spiral Matrix Reader

easy
MatrixArraySimulationIn Place Mutation
Asked atAmazonMicrosoftGoogleMeta

Problem Description

Given an `m x n` integer matrix `grid`, return all elements of the matrix in clockwise spiral order, starting from the top-left corner.

**Example 1:**
```
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
```

**Example 2:**
```
Input: grid = [[3,7,1,4],[2,6,8,5]]
Output: [3,7,1,4,5,8,6,2]
```

Constraints

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

Follow-up

Can you read the matrix in counter-clockwise spiral order with the same O(1) auxiliary space?

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.