Algorithm-rotate image (matrix processing)

Source: Internet
Author: User
Tags rotate image

Today in Lintcode did a problem, I think about the practice, made, but felt it necessary to record.

Test instructions

Given a nxn two-dimensional matrix to represent the image, 90 degrees clockwise rotation of the image.

Examples:

Given a rectangle [[1,2],[3,4]],90] Clockwise, return [[3,1],[4,2]]

Challenge:

Can you do it in situ?

1. Solving ideas

At first glance, this problem is really very simple. Here I use a graph to express the problem-solving ideas:

2. Code

1      Public voidRotateint[] matrix) {2         if(Matrix.length = = 0 | | matrix[0].length = = 0) {3             return;4         }5         //rotate clockwise 270 degrees6          for(inti = 0; i < matrix.length; i++) {7              for(intj = 0; J < I; J + +) {8                 inttemp =Matrix[i][j];9MATRIX[I][J] =Matrix[j][i];TenMatrix[j][i] =temp; One             } A         } -         //rotate counterclockwise 180 degrees -          for(inti = 0; i < matrix.length; i++) { the              for(intj = 0; J < Matrix[0].length/2; J + +) { -                 inttemp =Matrix[i][j]; -MATRIX[I][J] = matrix[i][matrix[0].length-1-j]; -Matrix[i][matrix[0].length-1-J] =temp; +             } -         } +}

Algorithm-rotate image (matrix processing)

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.