Rotate Image
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?
解題思路:
題目要求原地算法。假設坐標軸如上所示。順時針旋轉90度,相當於先將圖片沿著x=n/2翻轉,然後再將圖片沿著斜對角線y=x翻轉。代碼如下:
class Solution { public: void rotate(vector>& matrix) { int len = matrix.size(); //先將矩陣左右翻轉 for(int i = 0; i & v){ int len = v.size(); int i=0, j=len-1; while(i & v, int i, int j){ int temp = v[i]; v[i] = v[j]; v[j] = temp; } };