FastPrepRotate a Rectangular Matrix Clockwise

Rotate a Rectangular Matrix Clockwise

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a nonempty rectangular integer matrix with m rows and n columns, return its 90-degree clockwise rotation.

The returned matrix has n rows and m columns. Use only O(1) auxiliary space beyond the required output matrix.

Function

rotateRectangularMatrix(matrix: int[][]) → int[][]

Examples

Example 1

matrix = [[1,2,3],[4,5,6]]return = [[4,1],[5,2],[6,3]]

The two rows become the two columns read from bottom to top.

Example 2

matrix = [[1],[2],[3]]return = [[3,2,1]]

A 3x1 column becomes a 1x3 row.

Example 3

matrix = [[7,8,9,10]]return = [[7],[8],[9],[10]]

A 1x4 row becomes a 4x1 column.

Constraints

  • 1 <= matrix.length, matrix[0].length <= 300.
  • Every row has the same length.
  • -1000000000 <= matrix[i][j] <= 1000000000.

More Google problems

See Google hiring insights
public int[][] rotateRectangularMatrix(int[][] matrix) {
    // write your code here
}
matrix[[1,2,3],[4,5,6]]
expected[[4,1],[5,2],[6,3]]
Checking account…