Rotate Image
You are givenNXN2D matrix representing an image.
Rotate the image by 90 degrees (clockwise ).
Follow up:
Cocould you do this in-place?
Analysis: This question is a written question of Ali, to rotate in situ, you must find the law, matrix [I] [J] will rotate to the position of Matrix [J] [n-i-1, then turn I into J, J into n-i-1, continue Loop
Class solution {public: void rotate (vector <int> & matrix) {int n = matrix. size ()-1, I, J, K; for (I = 0; I <n; I ++) {for (j = I; I + j <N; j ++) {int x = I, y = J; // because the subscript must be changed later, use another variable int pre = matrix [x] [Y] here; // For val1-> val2, record the value of val1 int next; // record the value of val2 for (k = 0; k <4; k ++) // move four times each time {next = matrix [y] [n-X]; matrix [y] [n-x] = pre; Pre = next; int TMP = X; X = y; y = N-TMP ;}}}}};
Rotate image of leetcode