Leetcode-image (matrix) Rotation
Question:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise ).
Follow up:
Cocould you do this in-place?
Ideas:
The key to a question is in-place. Otherwise, it will be too easy. in order to achieve in-place, we can only use the space of Changshu variables and perform multi-variable cyclic rotation through two-variable exchange methods, that is, the form a-> B, B-> c, c-> a (-> represents the value assignment, the rest is how to use the geometric imagination to imagine how each node rotates in the matrix, and we need to summarize the relationships between the x-axis and Y-axis before and after 90 degrees of rotation. The relationships are as follows: (I, j) -> (j, N-1-i), where N is the size of the matrix, the rest is to pay attention to how to take the edge of the cycle, as long as do not create an infinite loop, you can start from any side. You can see from the amount of code that this question is very simple.
Code;
class Solution {public: void pointcirclerotate(vector
> &matrix, int i, int j) { int msize=matrix.size(); int tmp=matrix.at(i).at(j); matrix.at(i).at(j)=matrix.at(msize-1-j).at(i); matrix.at(msize-1-j).at(i)=matrix.at(msize-1-i).at(msize-1-j); matrix.at(msize-1-i).at(msize-1-j)=matrix.at(j).at(msize-1-i); matrix.at(j).at(msize-1-i)=tmp; } void rotate(vector
> &matrix) { int i,j,tmp; int msize=matrix.size(); for(i=0;i