Rotate image of leetcode

Source: Internet
Author: User
Tags rotate image

Rotate Image  

You are givenNXN2D matrix representing an image.

Rotate the image by 90 degrees (clockwise ).

Follow up:
Cocould you do this in-place?

Analysis: This question is a written question of Ali, to rotate in situ, you must find the law, matrix [I] [J] will rotate to the position of Matrix [J] [n-i-1, then turn I into J, J into n-i-1, continue Loop

Class solution {public: void rotate (vector <int> & matrix) {int n = matrix. size ()-1, I, J, K; for (I = 0; I <n; I ++) {for (j = I; I + j <N; j ++) {int x = I, y = J; // because the subscript must be changed later, use another variable int pre = matrix [x] [Y] here; // For val1-> val2, record the value of val1 int next; // record the value of val2 for (k = 0; k <4; k ++) // move four times each time {next = matrix [y] [n-X]; matrix [y] [n-x] = pre; Pre = next; int TMP = X; X = y; y = N-TMP ;}}}}};


Rotate image of 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.