Leetcode [48] (Java): Rotate Image

Source: Internet
Author: User
Tags rotate image

title : Matrix rotation

Difficulty : Medium

topic content :

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

Rotate the image by degrees (clockwise).

Note:

You had to rotate the image in-place, which means and you had to modify the input 2D matrix directly. Allocate another 2D matrix and do the rotation.

translation : You have a n*n 2D matrix that represents an image.

Rotates the image 90 degrees (clockwise).

Attention:

You have to rotate the image in place, which means you have to modify the input 2D matrix directly. Do not assign another 2D matrix and rotate it.

Example 1:

Given input matrix = [  [+], [  4,5,6],  [7,8,9]],rotate the input matrix in-place such that it becomes:[  [7,4,1],  [8,5,2],  [9,6,3]

Example 2:

Given input Matrix =[  [5, 1, 9,11],  [2, 4, 8,10],  [, 3, 6, 7],  [15,14,12,16]], rotate the input matrix In-place such that it becomes:[  [15,13, 2, 5],  [, 3, 4, 1], [[6], 8, 9  ],  [16, 7,10,11]]

my idea : rotation 90 °, that is, the matrix is divided into four blocks, only the first loop, and then the first element subscript "I" "j" expression of the corresponding elements of the other blocks, and then the four elements to exchange .

First, divide the matrix into the following four blocks:

A B

C D

Now loop through the elements in a, whose element subscript is denoted by "I" "J"

1. Store this element of a with temp.

2, a of this element to the C region corresponding to the element value, the corresponding element c is expressed as a element "first about the matrix of the main diagonal symmetry, and then take the longitudinal center fold"

  The main diagonal symmetry is "i" "j" = "J" "I"

  Vertical Center Fold , is "I" "j" = "n-1-i" "J"

3, Area C This element takes the value of the element in the D region, and the corresponding element in D is expressed as the center symmetry of a element, that is, "first about the transverse center fold, then take the longitudinal center fold"

  "I" "j" = "I" "n-1-J" in the middle of the horizontal line

4, D area This element takes the element value corresponding to the B region, and the corresponding element in area B is expressed as a element "first about the transverse center fold, then the matrix diagonal symmetry"

  Diagonal symmetry , "I" "j" = "N-1-j" "N-1-i"

5. Zone B This element takes the value of the previous zone A, that is, temp, to the end of this round of exchange.

It is important to note that when the size of the matrix is even, the symmetry center X of the number of rows at this time is one of the top of the symmetric line.

For example:

0

1

2

3 x = n-1 = 1, at this point the line range that needs to be swapped is "0~x", and the column range is "0~x"

If it is an odd number, then the line range is "0~x-1", the column range is unchanged or "0~x"

So the loop should be careful to judge the parity, and then skip.

My Code :

1      Public voidRotateint[] matrix) {2         intn =matrix.length;3         if(N <= 1) {4             return;5         }6         7         inteven = 1;8         if(matrix.length% 2 = = 1) {9even = 0;Ten         } One          A         intx = (n-1)/2; -          for(inti = 0; I <= x; i++) { -              for(intj = 0; J <= X; J + +) { the                 if(Even==0 && i==x) { -                     Continue; -                 } -                 inttemp =Matrix[i][j]; +MATRIX[I][J] = matrix[n-1-J] [i]; -Matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; +MATRIX[N-1-I][N-1-J] = matrix[n-1-(n-1-j)][n-1-i]; AMatrix[n-1-(n-1-j)][n-1-i] =temp; at             } -         } -}

my degree of complexity : O (N2)

problems in the encoding process :

1, should use the element of a subscript "I" "J" to represent all other blocks, rather than the relative position;

2, the "Symmetry" "fold" of the subscript changes are not familiar with, resulting in a lot of waste of time; (still want to use 2x-i + even to express, in fact directly n-1-i can)

3. The influence of the matrix parity on the cyclic range is not considered before, resulting in the odd symmetry line not being exchanged;

4, when the method definition is returned as void, you need to return directly, write the return line.

Answer code :

1      Public voidRotateint[] matrix) {2         intn =matrix.length;3         if(N <= 1) {4             return;5         }6         7         intx = (n-1)/2;8          for(inti = 0; I <= x; i++) {9             int[] tmp =Matrix[i];TenMatrix[i] = matrix[n-1-i]; OneMatrix[n-1-i] =tmp; A         } -          -          for(inti = 0; I < n; i++) { the              for(intj = i+1; J < N; J + +) { -                 inttemp =Matrix[i][j]; -MATRIX[I][J] =Matrix[j][i]; -Matrix[j][i] =temp; +             } -         } +}

complexity of the answer : O (N2)

Answer Ideas :

First, the entire array is swapped up and down symmetrically :

1 2 3     7 8 9 4 5 6  => 4 5 67 8 9     1 2 3

Then again about the main diagonal symmetric exchange :

7 8 9     7 4 14 5 6  => 8 5 21 2 3     9 6 3

Pros: Logic is clear

Cons: Complexity is a little higher than my approach

Extensions :

rotate 90° counterclockwise?

My method--abcd the value relationship between blocks changes.

The answer method--there are two ways to modify the line:

A: The first and second steps are exchanged, that is, the diagonal, then the fold

B: The first step of the upper and lower symmetrical exchange to the left and right symmetrical exchange, the second step unchanged.

Leetcode [48] (Java): 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.