【思路】要完成旋转共需两步
1.第一次x,y互换
s1[i][j]=s[j][i]
1, 2, 3, 45, 6, 7, 89, 10,11,1213,14,15,16变成了1, 5, 9, 132, 6,10,143, 7,11,154, 8,12,16然后x逆序
s2[i][j]=s1[n-i][j]
变成了4, 8,12,163, 7,11,152, 6,10,141, 5, 9, 13旋转成功1 void swap(int& x,int& y) 2 { 3 int temp = x; 4 x = y; 5 y = temp; 6 } 7 8 void rotate(vector< vector > &A) 9 {10 //A[i][j] = A[j][i]11 for(int i = 0;i < A.size();i ++)12 {13 for(int j = i;j < A[0].size();j ++)14 {15 swap(A[i][j], A[j][i]);16 }17 }18 19 //A[i][j] = A[row - i][j]20 for(int j = 0;j < A[0].size();j ++)21 {22 for(int i = 0;i < A.size()/2;i ++)23 {24 swap(A[i][j], A[A.size() - 1 - i][j]);25 }26 }27 28 }