This is the 28th day of my participation in the August Challenge

🌟 algorithm problem

Given an n by n two-dimensional matrix matrix represents an image. Please rotate the image 90 degrees clockwise.

You have to rotate the image in place, which means you need to modify the input two-dimensional matrix directly. Please do not use another matrix to rotate the image.

Example 1:

Input: matrix = [[1.2.3], [4.5.6], [7.8.9] output: [[7.4.1], [8.5.2], [9.6.3]]
Copy the code

Example 2:

Input: matrix = [[5.1.9.11], [2.4.8.10], [13.3.6.7], [15.14.12.16] output: [[15.13.2.5], [14.3.4.1], [12.6.8.9], [16.7.10.11]]
Copy the code
The sample3Input: matrix = [[1] output: [[1]]
Copy the code
The sample4Input: matrix = [[1.2], [3.4] output: [[3.1], [4.2]]
 
Copy the code
Matrix. Length == n matrix[I]. Length == n1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
Copy the code

🌟 A little thought

Don’t even think about actually moving an element in the array to that location, it’s too much trouble and they’re telling you that.

You have to rotate the image in place, which means you need to modify the input two-dimensional matrix directly. Please do not use another matrix to rotate the image.

So let’s look at the patternDid you find it?For the jj th element in row II of the matrix, after rotation, it appears at jj th position in the penultimate column II.Okay, so let’s code that

🌟 source code and detailed explanation

class Solution {
    public void rotate(int[][] matrix) {
    // Get the number of rows
        int n = matrix.length;
        // Create a two-dimensional array
        int[][] matrix_new = new int[n][n];
        For the jj element in row II of the matrix,
        // After the rotation, it appears at the jj position in the penultimate column II
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                matrix_new[j][n - i - 1] = matrix[i][j]; }}for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) { matrix[i][j] = matrix_new[i][j]; }}}}Copy the code

🌟 interview questions

Interviewer: Let’s talk about how Reflection works in Java

Reflection is god mode. If calling a method is the correct way to open Java, then reflection is god’s secret backdoor. As long as the corresponding class exists, everything can be called.

Then why did God open the back door? This involves the concept of static and dynamic

Static compilation: determine the type at compile time, binding object dynamic compilation: runtime to determine the type, the binding object The difference between the two is that dynamic compilation can maximum support polymorphism, the polymorphism of the greatest significance lies in the lower class coupling, so it is clearly the advantages of reflection: decoupling and improve the flexibility of the code.

Therefore, the advantages and disadvantages of reflection are:

advantage

Runtime type determination, dynamic class loading: improved code flexibility

disadvantage

Performance Bottleneck: Reflection is a series of interpreted operations that tell the JVM what to do, and performance is much slower than direct Java code

Application Scenario 1

JDBC database connection In JDBC operations, if you want to connect to the database, you must follow the above steps to complete

Load the database driver via class.forname () (via reflection, provided the relevant Jar package is introduced)

Use DriverManager to connect to the database. When connecting, enter the database connection address, user name, and password

Receive the Connection through the Connection interface

2. Use of the Spring framework

Spring’s process for loading beans through XML configuration schema:

Load all the XML or Properties configuration files in the program into an in-memory Java class and parse the contents of the XML or Properties, get the bytecode strings of the corresponding entity class and the related property information using reflection mechanism, Based on this string, the Class instance of a Class is dynamically configured for instance properties