Remember an interesting algorithmic problem rotate image (rotate image)

Source: Internet
Author: User
Tags rotate image

The topics from https://leetcode.com/problems/rotate-image/are:

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

Rotate the image by degrees (clockwise).

Follow up:
Could do this in-place?

In a nutshell, a two-dimensional array of n*n is given, and the array is rotated 90 degrees clockwise, and no additional storage space is used.

The first thing that came to mind was figuring out the rotation of each coordinate. Suppose we are the matrix of 2*2:

 
   a BC D

After the rotation, it becomes:

 
   C AD B

So it's a matter of switching to 4 numbers instead of using extra space. The most common algorithm for exchanging numeric values without using extra space is XOR, for example, to exchange a value of a A and B, it can be written as:

 
   a = a^b ; b = a^b ; a = a^b ;

Now is the rotation of 4 numbers, the result of rotation is a=c,b=a,c=d,d=b;

So rewrite the algorithm to be different or, then it is:

  a=a ^ b ^ C ^ D;b=a ^ b ^ C ^ D;D=a ^ b ^ C ^ D;C=a ^ b ^ C ^ D;a=a ^ b ^ C ^ D;

The next step is to find out the relationship between the angular mark and a,b,c,d in the two-dimensional array, which is not difficult. In addition, we only have to deal with 1/4 of the area when we are doing the rotation process, because the processing is adjusted by 4 number, so we only deal with the value of the upper left corner of the two-dimensional array.

Here is the specific code:

   Public voidRotate (int[,] matrix) {    intN=Matrix. GetLength (0);  for(var i= 0; I<(n+ 1)/2; I++)    {         for(Var j= 0; J<N/2; J++)        {            //var a = Matrix[i, j]; //var b = matrix[j, n-i-1]; //var d = matrix[n-i-1, n-j-1]; //var c = matrix[n-j-1, I];Matrix[i, J]=Matrix[i, J]^matrix[j, N-I- 1] ^Matrix[n-I- 1, N-J- 1] ^Matrix[n-J- 1, I]; MATRIX[J, N-I- 1] =Matrix[i, J]^matrix[j, N-I- 1] ^Matrix[n-I- 1, N-J- 1] ^Matrix[n-J- 1, I]; Matrix[n-I- 1, N-J- 1] =Matrix[i, J]^matrix[j, N-I- 1] ^Matrix[n-I- 1, N-J- 1] ^Matrix[n-J- 1, I]; Matrix[n-J- 1, I]=Matrix[i, J]^matrix[j, N-I- 1] ^Matrix[n-I- 1, N-J- 1] ^Matrix[n-J- 1, I]; Matrix[i, J]=Matrix[i, J]^matrix[j, N-I- 1] ^Matrix[n-I- 1, N-J- 1] ^Matrix[n-J- 1, I]; }    }}

The use of XOR is not intuitive, and a more intuitive way to exchange two data is by adding and subtracting:

 
   a = a + b;b = a - B;a = a - b;

We use XOR instead of using more intuitive addition and subtraction because the a+b may overflow, then the result is wrong, so you cannot add and subtract and you should use XOR.

Remember an interesting algorithmic problem rotate image (rotate image)

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.