Java implements a rotating image algorithm: given an n×n two-dimensional matrix, rotate it clockwise by 90°

tags: algorithm

Rotating image algorithm: given an n×n two-dimensional matrix, rotate it clockwise by 90°
For example:
Enter:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Output:
[7, 4, 1]
[8, 5, 2]
[9, 6, 3]

enter:
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[13, 14, 15, 16]
output:
[13, 9, 5, 1]
[14, 10, 6, 2]
[15, 11, 7, 3]
[16, 12, 8, 4]

Thinking analysis:
1. Obviously use a two-dimensional array to solve the problem. Here we use a matrix of 1 to 9 total 9 digits as an example to traverse
2. First, in the input, the positions of 123 in the two-dimensional array are:[0][0],[0][1],[0][2]
3. Secondly in the output, the positions of 123 in the two-dimensional array are:[0][2],[1][2],[2][2]
4. When the array traverses the outer loop i=0, temp[j][3-0-i]=matrix[i][j], j takes the value 0~2, and determines 3 Number
5. When the array traverses the outer loop i=1, temp[j][3-0-i]=matrix[i][j], j takes the value 0~2, and determines 3 Number
6. When the array traverses the outer loop i=2, temp[j][3-0-i]=matrix[i][j], j takes the value 0~2, and determines 3 Number
7. The law will come out

The code sheet is as follows

 public static int[][] rotate(int[][] matrix) {
        int len=matrix.length;
        int[][] temp = new int[len][len];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                temp[j][len - i - 1] = matrix[i][j];
            }
        }
        return temp;
    }

start testing:

 public static void main(String[] args) {
        int[][] matrix0 = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}};
        int[][] matrix = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};
        print(matrix);
        System.out.println("-----------");
        print(rotate(matrix));

    }

    public static void print(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            System.out.println(Arrays.toString(matrix[i]));
        }
    }

Output:

[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[13, 14, 15, 16]
-----------
[13, 9, 5, 1]
[14, 10, 6, 2]
[15, 11, 7, 3]
[16, 12, 8, 4]

Intelligent Recommendation

LeetCode ---- gives a result of returning the image with a two-dimensional matrix to rotate 90 degrees clockwise

Topic description Give an image represented by a two-dimensional matrix Returns the result of 90 degrees clockwise clockwise Expansion: Can you use the original place algorithm to solve this problem? ...

Rotate a two-dimensional array (square matrix) 90° clockwise

Questions: Procedure list: operation result:...

Data—rotate an N*N matrix 90 degrees clockwise

problem 1.6 Problem: Rotate an N*N matrix clockwise by 90 degrees (other shapes such as 180 degrees clockwise or 90 degrees counterclockwise) do not take up extra space. achieve Rotate one by one acco...

Java Rotating Matrix | Give you an image represented by the N × N matrix, where each pixel is 4 bytes. Please design an algorithm to rotate the image 90 degrees. Can you do any additional memory space?

Rotating matrix Give you aN × NThe image represented by the matrix, wherein each pixel is 4 bytes. Please design an algorithm to rotate the image 90 degrees. Can you do any additional memory spa...

More Recommendation

Brush title No17. rotate-image (N*N matrix image rotates the image clockwise) (java) [array]

topic: Given an image represented by a two-dimensional matrix, return the result of rotating the image 90 degrees clockwise. Examples are as follows: Perform rotation assignment exchange. Code:  ...

Rotate a two-dimensional array 90° clockwise

Before rotatingAfter rotating Transpose the array first, and then reverse the order of the columns. Transpose: Swap elements in symmetrical positions along the main diagonal Reverse order: Exchange th...

Rotate the two -dimensional group square and clockwise 90 °

Brief introduction I don't feel very intuitive on the Internet to describe this algorithm. So I simply write one by myself legend The rotation is shown in the figure, and I put the sideline on the ser...

Clockwise 90 ° rotating matrix

Rotate image step: Turn along the main diagonalq[i][j] <==> q[j][i] Turn along the center axis - There is another kind of thing below to see that code is good. Method Two: Up and down Digital an...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top