Rotate Image Leetcode

Source: Internet
Author: User

Problem:

You are given a n x n 2D matrix representing an image.

Rotate the image by degrees (clockwise).

Follow up:could does this in-place? Analysis: Two-dimensional array a[n][n] clockwise rotation 90 degrees, to solve this problem, undoubtedly, the first thing is to find the law. When N=1, don't move. When n=2, after rotation: a[0][0] = a[1][0]a[1][0] = a[1][1]a[1][1] = a[0][1]a[0][1] = A[0][0] Here we have a preliminary summary of the law: a[i][j] = a[n-1-j][i] When n=3, After the rotation apparently also satisfies the above law when n=4,5,...... is also satisfied. Here, if you don't consider the complexity of the space, we can solve this problem, only need to build a two-dimensional array b[n][n], using the formula B[i][j] = A[n-1-j][i], OK, the code is as follows:
voidRotate (vector<vector<int>>&matrix) {    intn =matrix.size (); Vector<vector<int>>m;  for(introw =0; Row < n; ++row) {         for(intCol =0; Col < n; ++Col) {M[row][col]= Matrix[n-1-Col]        [Row]; }    }    //re-assigned back to Matrix     for(introw =0; Row < n; ++row) {         for(intCol =0; Col < n; ++Col) {Matrix[row][col]=M[row][col]; }    }}
But here, the topic is also asked, in the original array, how should be rotated? Then the above analysis, taking n=3 as an example, we focus on the rotation of an element, we can see to rotate in the member array, in the case of no loss of data, the rotation of each value will "ripple" 4 numbers, with 1 as an example ripple to 1, 3,7,9, each number of rotation or loss of data will be considered how to make this 4 number can be preserved before summing up the law a[i][j] = a[n-1-j][i], analysis of each group is affected by the number, we can draw the number of 4 in this case is actually a[i][j]a[n-1-j][i]a[ N-1-i][n-1-j]a[n-1-(N-1-J)][n-1-i]=a[j][n-1-i] So here we need to introduce a temporary variable temp to resolve these 4-digit clockwise interchanges, such as: int temp = 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] = temp; focus on an element, the number of exchange problem solved, then we focus back to the whole two-dimensional array, the rotation of each number will ripple 4 number, equivalent to the above method, each rotation of a number, a group of 4 numbers are rotated, So now the question is how to complete all the numbers are rotated 90 degrees and not much rotation, continue to analyze it, n=1, do not need to rotate. n=2, you only need to complete the rotation of 1 (a[0][0]) to complete the rotation of the entire array. N=3, you need to complete the rotation of the a[0][0],a[0][1] to complete the rotation of the entire array n=4 when you need to complete the 1,2,3,6 (a[0][0 to 3],a[1][1]) rotation n=5, need to complete (a[0][0 to 4],a[1][1 to 2 ]) can roughly summarize a rule: for the number of a[i][j to be rotated, I<N/2 and I<=j<n-1-iAt this point the problem has finally been solved perfectly. The code is as follows:
voidRotate (vector<vector<int>>&matrix) {        intn =matrix.size (); intLimit = (N-1) >>1;  for(inti =0; I <= limit; ++i) {             for(intj = i; J < N-1I ++j) {                inttemp =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.] =temp; }        }    }

Rotate Image Leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.