Written question 61. Leetcode OJ (48)

Source: Internet
Author: User

The problem is to give you a 2D image (we know that the 2D image is made up of (x, y) pixels), the topic requires us to rotate the graph clockwise 90 degrees, is indeed a more interesting topic.

If this is the first time you've got the problem, it's really hard to do it, but once you've seen a similar topic (for example, the spiral array on the offer), then you have a moment of thought. Let me draw a diagram to explain the idea of the problem:


The main idea is also from the outside, each rotation off a layer, repeat the above operation, know the loop to the innermost, this time completed the overall rotation. Code:

Class Solution {Public:void rotate (vector<vector<int>>& matrix) {int num = matrix.size (); for (int i = 0; i < NUM/2; ++i) {//rotate one layer at a time rotateone (matrix, I);}} void Rotateone (vector<vector<int>>& matrix, int begin) {int len = matrix.size ()-2 * begin-1;int tmp = 0 ; for (int i = len; I >= 1; i) {//rotate TMP = matrix[begin][begin + I];matrix[begin][begin + i] = Matrix[begin + len-i][ Begin];matrix[begin + Len-i][begin] = matrix[begin + len][begin + len-i];matrix[begin + len][begin + len-i] = Matrix [Begin + I] [begin + Len];matrix[begin + i][begin + len] = tmp;}};
Results:



Written question 61. Leetcode OJ (48)

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.