Rotate image <leetcode>

Source: Internet
Author: User
Tags rotate image

You are givenNXN2D matrix representing an image.

Rotate the image by 90 degrees (clockwise ).

Follow up:
Cocould you do this in-place?

 

Train of Thought: First Flip left and right, and then flip right diagonal lines in the lower left corner. The Code is as follows:

 

 1 class Solution { 2 public: 3     void rotate(vector<vector<int> > &matrix) { 4         int n=matrix.size(); 5         if(n==1)  return; 6         for(int i=0;i<n;i++) 7         { 8             for(int j=0;j<n/2;j++) 9             {10                 swap(matrix[i][j],matrix[i][n-j-1]);11                 12             }13         }14         for(int i=0;i<n;i++)15         {16             for(int j=0;j<n-i;j++)17             {18                 swap(matrix[i][j],matrix[n-j-1][n-i-1]);19             }20         }21     }22 };

 

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.