⚡ Daily algorithm & interview questions ⚡ Study together 8 ️⃣

Keywords: Java Algorithm Interview

⚽ Digression

Yesterday, a little friend came to me and asked me a lot of questions. What can I do? After understanding, I found that I don't know much about the data structure. I don't know much about how to build linked lists and stacks. I suggest you look at the data structure and fill it up. Let's brush the questions slowly. As for how to fill it up, if the little partner doesn't have a good plan, you can also talk about me in private. I'll find resources for you.

🌟 Algorithm problem

Given an array num without duplicate numbers, returns all possible permutations. You can return answers in any order.

Example 1:

Input: nums = [1,2,3]
Output:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:

Input: nums = [0,1]
Output:[[0,1],[1,0]]
Example 3:

Input: nums = [1]
Output:[[1]]
 
Tips:

1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums All integers in are different from each other

🌟 A little thought

I think the idea of this problem is quite clear. You can see that the case of num = [0,1] has [[0,1], [1,0]] which is equivalent to traversing all the cases, but ensure that the elements are not repeated. Li is this Li, but how to realize it?, Is it difficult for you to achieve it all from the first pass to the last? This is exponential. The simplest methods are recursion and backtracking. Let's talk about backtracking today. It appears many times in our question.

Backtracking: an algorithm that finds all solutions by exploring all possible candidate solutions. If the candidate solution is confirmed to be not a solution (or at least not the last solution), the backtracking algorithm will discard the solution by making some changes in the previous step, that is, backtracking and try again

🌟 Source code and detailed explanation

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
//Encapsulated as a list for later operations, because there are few methods available for arrays
        List<Integer> output = new ArrayList<Integer>();
        for (int num : nums) {
            output.add(num);
        }

        int n = nums.length;
        backtrack(n, output, res, 0);
        return res;
    }
//n is the length of nums. res is used to store the results. first is used to store the results
    public void backtrack(int n, List<Integer> output, List<List<Integer>> res, int first) {
        // The core of backtracking and recursion are almost the same. Be sure to judge the ending conditions
        //All the numbers have been filled in
        if (first == n) {
            res.add(new ArrayList<Integer>(output));
        }
        for (int i = first; i < n; i++) {
            // Dynamic maintenance array
            Collections.swap(output, first, i);
            // Continue to fill in the next number recursively
            backtrack(n, output, res, first + 1);
            // Undo operation
            Collections.swap(output, first, i);
        }
    }
}

🌟 Interview questions

Today we talk about yesterday's MyBatis. Yesterday we talked about his 1. What is it, 2. Advantages and 3. Disadvantages
Today we talk about other features of

1. Where MyBatis framework is applicable: MyBatis focuses on SQL itself and is a sufficiently flexible DAO layer solution.

MyBatis will be a good choice for projects with high performance requirements or more demand changes, such as Internet projects.

2. What are the differences between MyBatis and Hibernate?
Unlike hibernate, mybatis is not entirely an ORM framework, because mybatis requires programmers to write Sql statements themselves.

Mybatis directly writes the original sql, which can strictly control the sql execution performance and has high flexibility. It is very suitable for software development with low requirements for relational data model, because the requirements of this kind of software change frequently, but once the requirements change, it requires rapid output of results. However, the premise of flexibility is that mybatis cannot be database independent. If you need to implement software that supports multiple databases, you need to customize multiple sets of sql mapping files, which is a heavy workload.

Hibernate has strong object / relational mapping ability and good database independence. For software with high requirements for relational model, if developed with hibernate, it can save a lot of code and improve efficiency.

3. What is the difference between #{} and KaTeX parse error: Expected 'EOF', got '#' at position 11: {}# ̲ {} is precompiled processing and {} is string substitution.

When Mybatis processes #{}, it will replace #{} in sql with #{}? Number, call the set method of PreparedStatement to assign value;

Mybatis is dealing with Time , Just yes hold When {} is When {} is replaced by the value of the variable.

Using #{} can effectively prevent SQL injection and improve system security.

Special introduction

📣 Xiaobai training column is suitable for newcomers who have just started. Welcome to subscribe Programming Xiaobai advanced

📣 The interesting Python hand training project includes interesting articles such as robot awkward chat and spoof program, which can make you happy to learn python Training project column

📣 In addition, students who want to learn java web can take a look at this column: Teleporters

📣 This is an algorithm exercise for interview and postgraduate entrance examination. Let's refuel together The way ashore

Posted by velanzia on Wed, 22 Sep 2021 16:17:14 -0700