leetcode@ [48] Rotate Image
https://leetcode.com/problems/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?
class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); if(n == 0) return; for(int layer = 0; layer < n/2; ++layer) { int first = layer, last = n-1-layer; for(int i = first; i < last; ++i) { int offset = i - first; //save the top. int top = matrix[first][i]; //left -> top. matrix[first][i] = matrix[last-offset][first]; //buttom -> left. matrix[last-offset][first] = matrix[last][last-offset]; //right -> buttom. matrix[last][last-offset] = matrix[i][last]; //top -> right. matrix[i][last] = top; } } } };