Skip to main content

Matrix Diagonal Flip

easy
MatrixArrayIn Place Mutation
Asked atAmazonMicrosoftApple

Problem Description

Given a square integer matrix `board` of size `n x n`, **transpose** it in place — i.e., reflect every element across the main diagonal so that `board[i][j]` becomes `board[j][i]`.

Return the modified matrix.

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

**Example 2:**
```
board = [
[12, 0],
[-3, 7]
]
```
Output:
```
[
[12, -3],
[0, 7]
]
```

Constraints

  • 1 <= n <= 500
  • -10^5 <= board[i][j] <= 10^5
  • The matrix is guaranteed to be square (n x n).

Follow-up

After transposing, rotate the matrix 90 degrees clockwise in place by then reversing each row — achieving a full in-place rotation with no extra 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.