Daily algorithm: Rotate Image (Image rotation)

Source: Internet
Author: User
Tags rotate image

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?

The original image rotates 90 degrees clockwise. Because the space complexity is a constant, iteration rotation should be performed.

Class Solution {public: void rotate (vector
 
  
> & Matrix) {int n = matrix. size (); int layers = n/2; // Number of circles in the image rotation for (int layer = 0; layer <layers; layer ++) // layer of each loop, right cyan to purple for (int I = layer; I
  
   
The following is another solution provided by netizens:


Class Solution {public: void rotate (vector
    
     
> & Matrix) {int I, j, temp; int n = matrix. size (); // reverse the for (int I = 0; I <n; ++ I) {for (int j = 0; j <n-I; ++ j) {temp = matrix [I] [j]; matrix [I] [j] = matrix [n-1-j] [n-1-I]; matrix [n-1-j] [n-1-I] = temp ;}// reverse along the horizontal midline for (int I = 0; I <n/2; ++ I) {for (int j = 0; j <n; ++ j) {temp = matrix [I] [j]; matrix [I] [j] = matrix [n-1-I] [j]; matrix [n-1-I] [j] = temp ;}}}};
    


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.