# Rotate Matrix

## Question ([LC.48](https://leetcode.com/problems/rotate-image/))

> Given a mxn matrix, can you rotate it clockwise and counter-clockwise? Can you do it in place?

## Analysis

There are two approaches. One way to do it layer by layer. The second approach requires some linear algebra knowledge. Here we'll discuss the second approach - transposing and flipping the matrix.

## Transpose

Transpose the matrix first n x m result\[x]\[y] = matrix\[y]\[x] m x n

```java
private int[][] transpose(int[][] matrix) {
    int m = matrix.length;
    int n = matrix[0].length;
    int[][] result = new int[n][m];

    for (int y = 0; y < m; y++) {
        for (int x = 0; x < n; x++) {
            result[x][y] = matrix[y][x];
        }
    }

    return result;
}
```

## Vertical/Horizontal Flip

If rotate clockwise, flip horizontally.

If rotate counterclockwise, flip vertically.

```java
private void horizontalFlip(int[][] matrix) {
    int m = matrix.length;
    int n = matrix[0].length;

    for (int y = 0; y < m; y++) {
        for (int x = 0; x < n/2; x++) {
            int temp = matrix[y][x];
            matrix[y][x] = matrix[y][n - 1 - x];
            matrix[y][n - 1- x] = temp;
        }
    }
}

private void verticalFlip(int[][] matrix) {
    int m = matrix.length;
    int n = matrix[0].length;

    for (int x = 0; x < n; x++) {
        for (int y = 0; y < m/2; y++) {
            int temp = matrix[y][x];
            matrix[y][x] = matrix[m - 1 - y][x];
            matrix[m - 1 - y][x] = temp;
        }
    }
}
```

## Rotate Matrix

```java
public int[][] rotateMatrix(int[][] matrix, int flag) {
    if (matrix == null || matrix.length == 0)
        return matrix;
    if (matrix[0] == null || matrix[0].length == 0)
        return matrix;
    int[][] result;
    result = transpose(matrix);

    if (flag == 1) // rotate clockwise
        horizontalFlip(result);
    else // rotate counterclockwise
        verticalFlip(result);
    return result;
}
```

## Note

Rotation in-place has very limited options in terms of operation. You pretty much have to figure out a way that only uses swapping elements and holding a temp variable. Swapping horizontally, vertically, diagonally, multiple times (with different substrings), etc.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zedive.gitbook.io/project-l/part-1/basic_data_structure/array_and_string/math/rotate-matrix.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
