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?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Solution {public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for(int i = 0; i < n/2; ++i){ for(int j = i; j < n - 1 -i; ++j){ int tmp = matrix[i][j]; matrix[i][j] = matrix[n-1-j][i]; matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; matrix[n-1-i][n-1-j] = matrix[j][n-1-i]; matrix[j][n-1-i] = tmp; } } }}; |
1 2 3 4 5 6 7 8 9 10 11 12 | class Solution {public: void rotate(vector<vector<int>>& m) { int n = m.size(); for(int i=0; i<n; i++) for(int j=0; j<i; j++) swap(m[i][j], m[j][i]); for(int i=0; i<n; i++) reverse(m[i].begin(), m[i].end()); }}; |
原文:http://www.cnblogs.com/zhxshseu/p/584faf35ab38d775029885ef8788db53.html