LeetCode_Rotate Image, leetcode_rotate
I. QuestionsRotate Image Total Accepted:32380Total Submissions:102010My Submissions
You are givenNXN2D matrix representing an image.
Rotate the image by 90 degrees (clockwise ).
Follow up:
Cocould you do this in-place?
Show TagsHave you met this question in a real interview? Yes No
Discuss
Ii. problem-solving skillsSince the image must be rotated 90 degrees clockwise, the simplest way is to draw a picture to observe the image after 90 degrees of rotation. After analysis, we can see that, rotating 90 degrees clockwise is to take the last row of the original image as the first column, the last row as the second column, and so on, the first row as the last column. If we use this method directly, it is difficult to find the relationship between the coordinates of the original image and the coordinates corresponding to the rotation, you can only create a temporary image with a size of n * n for copying, and then copy the rotated image back to the original image. The time complexity of this approach is O (n ^ 2), and the space complexity is O (n ^ 2 ). Regardless of the angle of the image, the point in the upper left corner can be regarded as the origin of the coordinate system. Therefore, the original image is abstracted as a rectangle with four vertices ranging from 1 to 2-4, after 90 degrees clockwise rotation, the image is abstracted as four rectangles with 4-1-2-3 vertices. For this result, you can perform horizontal symmetry on the original image first. In this way, the image is abstracted as 2-1-4-3, then, the horizontal symmetric image is symmetric along the diagonal line, and the abstract is 4-1-2-3. Then, the result after 90 degrees of rotation is obtained. The specific process is as follows:
The above method first performs horizontal symmetry on the image, and then performs 45-degree symmetry. the time complexity is O (n ^ 2), and the space complexity is O (1 ).
Iii. Implementation Code
# Include <iostream> # include <vector> using namespace std; class Solution {private: void Mirror (vector <int> & matrix) {const int N = matrix. size (); const int Half = N/2; for (int IndexOfRows = 0; IndexOfRows <N; IndexOfRows ++) {for (int IndexOfCols = 0; IndexOfCols <Half; indexOfCols ++) {int Tmp = matrix [IndexOfRows] [IndexOfCols]; matrix [IndexOfRows] [IndexOfCols] = matrix [IndexOfRows] [N-1-IndexOfCols]; matrix [IndexOfRows] [N-1-IndexOfCols] = Tmp ;}} void RotateFour (vector <int> & matrix) {const int N = matrix. size (); for (int IndexOfRows = 0; IndexOfRows <N-1; IndexOfRows ++) {for (int IndexOfCols = 0; IndexOfCols <N-1-IndexOfRows; indexOfCols ++) {int Tmp = matrix [IndexOfRows] [IndexOfCols]; matrix [IndexOfRows] [IndexOfCols] = matrix [N-1-IndexOfCols] [N-1-IndexOfRows]; matrix [N-1-IndexOfCols] [N-1-IndexOfRows] = Tmp ;}}public: void rotate (vector <int> & matrix) {// horizontal symmetric Mirror (matrix); // 45 degree symmetric RotateFour (matrix );}};
Iv. ExperienceThis question is a little tricky. because it needs to be performed in-place, I understand that the space complexity is O (1). Therefore, we should consider the relationship between the rotated image and the original image. According to the above analysis, we can see that the rotated image first performs horizontal symmetry on the original image, then perform 45-degree symmetry. Therefore, symmetric operations can be performed in-place. Therefore, according to the above method, the space complexity is O (1 ).
All rights reserved. You are welcome to reprint it. Please indicate the source for reprinting. Thank you.