Question:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?to see which companies asked this question
可以先旋转最外围一层,然后依次旋转里层:
1 class Solution { 2 public: 3 void rotate(vector>& matrix) { 4 int N=matrix.size(); 5 for(int layer=0;layer top14 matrix.at(last-offset).at(first)=matrix.at(last).at(last-offset); //bottom->left15 matrix.at(last).at(last-offset)=matrix.at(i).at(last); //right->bottom16 matrix.at(i).at(last)=top; //top->right17 }18 }19 20 }21 };