Leetcode Rotate Image

Source: Internet
Author: User

You are given a n x n 2D matrix representing an image.

Rotate the image by degrees (clockwise).

Follow up:
Could do this in-place?

Idea: The matrix is considered to be surrounded by a layer of multiple squares. When you turn the side of the square, as long as the side of each number of the next to replace the good, four directions that is four times to replace each layer in turn.

Total number of layers = MATRIX.LENGTH/2, layer I starts from matrix[i][i] to matrix[i][n-1-1-i], note save temp = Matrix[i][i], ensure that the last substitution value is not overwritten, four substitutions in four directions per round

 Public classS048 { Public voidRotateint[] matrix) {        //find the rules and replace each of the four directions in each layer of the matrix in turn        if(Matrix.length = = 1)            return ; intLen =matrix.length;  for(inti = 0;i<len/2;i++) {             for(intj = i;j<len-1-i;j++) {                inttemp =Matrix[i][j]; MATRIX[I][J]= matrix[len-1-J]                [i]; Matrix[len-1-j][i] = matrix[len-1-i][len-1-J]; Matrix[len-1-I][LEN-1-J] = matrix[j][len-1-i]; Matrix[j][len-1-i] =temp; }        }    }     Public Static voidMain (string[] args) {S048 s=NewS048 (); int[] nums= {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};    S.rotate (Nums); }}

Leetcode Rotate Image

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.