← 返回 google 的题目列表Rotate Any n×m Matrix
类型:online_judge
Given an n x m 2D matrix mat, rotate it 90 degrees clockwise and return the rotated matrix.
The solution must work for any dimensions (not necessarily square).
Input
First line: two integers n m
Next n lines: m integers each
Output
Print the rotated matrix: m lines, each with n integers
Example
Input:
2 3
1 2 3
4 5 6
Output:
4 1
5 2
6 3
Constraints
1 <= n, m <= 2000
Follow-up
Discuss whether O(1) extra space is possible:
For n != m, you typically need a new matrix.
For n == m, in-place rotation can be discussed.
Example
Input
2 3
1 2 3
4 5 6
Output
4 1
5 2
6 3