Type:medium
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Giveninput matrix= [ [1,2,3], [4,5,6], [7,8,9]],rotate the input matrixin-placesuch that it becomes:[ [7,4,1], [8,5,2], [9,6,3]]
本题题意为给定一个n*n数组,按照时钟方向旋转90度,输出旋转后的数组,且不能建立其他的二维数组。
此题通用方法:
1、reverse第一个数组到最后一个数组
2、对右上角三角形区域,做matrix[i][j]与matrix[j][i]换值
若是反时钟方向,则是对左上角三角形区域做转换。
void rotate(vector
int n = matrix.size();
reverse(matrix.begin(), matrix.end());
for(int i=0; i for(int j=i+1; j swap(matrix[i][j], matrix[j][i]); }