Leetcode-image (matrix) Rotation

Source: Internet
Author: User

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
   
    

 

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.