Leetcode -- rotate image (two-dimensional array rotates 90 degrees clockwise)

Source: Internet
Author: User
Tags rotate image

Question:

You are givenNXN2D matrix representing an image.

Rotate the image by 90 degrees (clockwise ).

Follow up:
Cocould you do this in-place?

Analysis:

The two-dimensional array a [n] [N] rotates 90 degrees clockwise. To solve this problem, the first thing is to find the law.

When n = 1, no need to change.

When n = 2,

Changes

Include:

A [0] [0] = A [1] [0]

A [1] [0] = A [1] [1]

A [1] [1] = A [0] [1]

A [0] [1] = A [0] [0]

Here we will preliminarily summarize the law as: A [I] [J] = A [n-1-j] [I]

When n = 3,

Changes

Apparently, the rule above is met.

When N is 4, 5 ,...... Is also satisfied.

By now, we can solve this problem without considering the complexity of the space. We only need to construct another two-dimensional array B [N] [N]. using the formula B [I] [J] = A [n-1-j] [I], it is OK, the Code is as follows:

Public void rotate (INT [] [] matrix) {int n = matrix. length; int [] [] M = new int [N] [N]; for (int row = 0; row <n; row ++) {for (INT Col = 0; Col <n; Col ++) {M [row] [col] = matrix [n-1-col] [row] ;}} // then assign a value back to matrix. Note that Java is a reference passed for (int row = 0; row <n; row ++) {for (INT Col = 0; col <n; Col ++) {matrix [row] [col] = m [row] [col] ;}}

But here, the question also requires that, in the original array, how should we rotate?

The above analysis takes n = 3 as an example:

Changes

We place the focus on the rotation of an element. We can see that to rotate in the member array, the rotation of each value will "affect" 4 times without losing data, taking 1 as an example to reach 1, 3, 7, and 9. If each number is rotated or data loss occurs, we must consider how to keep the four numbers.

The front side summarizes the law a [I] [J] = A [n-1-j] [I], analysis of the number of each group is affected, we can conclude that here the number of 4 is actually

A [I] [J]

A [n-1-j] [I]

A [n-1-i] [n-1-j]

A [n-1-(n-1-j)] [n-1-i] = A [J] [n-1-i]

So we need to introduce a temporary variable temp to solve the clockwise switching of the four numbers, such:

int temp = 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[j][n-1-i];matrix[j][n-1-i] = temp;

The focus is on one element, and the number exchange problem is solved,

Now we will focus on the whole two-dimensional array. The rotation of each number will affect four, which is equivalent to the above method, the four numbers in a group are rotated,

So now the question is how to completely rotate all the numbers 90 degrees and not rotate much. Continue the analysis,

If n = 1, no rotation is required.

N = 2,

You only need to complete the 1 (a [0] [0]) rotation to complete the entire array rotation.

N = 3,

You need to complete the 1, 2 (A [0] [0], a [0] [1]) rotation to complete the entire array rotation.

N = 4,

The rotation of 1, 2, 3, 6 (A [0] [0 to 3], a [1] [1]) needs to be completed.

N = 5,

Required (a [0] [0 to 4], a [1] [1 to 2])

This rule can be summarized as follows:

For the number a [I] [J] to be rotated,

I <n/2

And

I <= j <n-1-i

The problem is finally solved perfectly ..

The Code is as follows:

public class Solution {    public void rotate(int[][] matrix) {        int n = matrix.length;        int limit = (n-1)/2;        for(int i=0;i<= limit; i++){            for(int j=i;j<n-1-i;j++){                int temp = 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[j][n-1-i];                matrix[j][n-1-i] = temp;            }        }    }}

Leetcode -- rotate image (two-dimensional array rotates 90 degrees clockwise)

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.