Offer to come, dig friends take it! I am participating in the 2022 Spring Recruitment series of activities – Experience review, click to view the details of the activity.

The cause of

Although I tried to brush for a period of time before I changed my job last year, I never recorded my brushing process. It wasn’t until May 2021, when the Nuggets hosted a Java swipe card event, that I started Posting my first swipe article on the Nuggets 👇

Later, The Nuggets held the article challenge activity in June 2021, and 30 articles were required for the grand prize. In this activity, I finally output 23 articles with the ability to brush the topic.

So far, I have published 124 force buckle problems on the Nuggets, if you are interested, please follow my column: force buckle brush problems

What did I do?

1. Code storage

In order to debug the code more easily and store my own test cases, I built a private library on Github to store the code. The advantage of this is that you can get your history code anywhere, and you can keep your own test cases. I highly recommend that you also use a repository to store your own problem solving code!

2. Imitate and summarize

At the beginning, I only knew how to solve the problem by violence, so the defeat rate was less than 10% when I submitted the first problem. Later, after reading the official solution, I found that I could use HashMap to exchange space for time (because the linked list nodes in HashMap would turn into a red-black tree if they exceeded 8).

So in the beginning, I read the problem for a long time -> thought for 15 minutes, no idea -> read the official solution, but after a period of time, I found that my improvement was very small. It is like a brain will, the hand will not feel, the next time encounter similar problems or will not do.

Later, after constant groping, I brush the steps are like this:

  1. Look at the title and list the main points in the title
  2. Figure out the key and idea of solving the problem (I usually draw on scratch paper for linked list or binary tree related problems)
  3. Really can’t see the official problem solution, but do not copy the official code
  4. Run the debugAfter writing the code, write the test case of the force button in the main method to test (like some binary tree questions need to be converted according to the official binary tree array itselfTreeNodeA)
  5. Submit the code and then debug the failed test cases locally (usually about 20 minutes, don’t worry!).
  6. Until all the test cases pass

After this period of time brush, I also have some small comprehension:

  • The essence of the algorithm is the use of space switching time
  • sometimesYou can look at the data structure and guess what algorithm to useCome across, for exampleBinary treeWill think ofrecursiveAnd metIntercept stringWill think ofThe sliding windowEncounter a big problem can be decomposed situation will think ofDynamic programming
  • useHashMapInstead of arrays or linked lists, time complexity can be effectively reduced
  • When traversing a linked list, you usually need a temporary pointer to the head node
  • .

What have I gained?

Although brush problem encountered some algorithms can’t very well in the actual business fall to the ground, but to brush the topic can have a more profound understanding of data structure, the ability of the debugging (to break step down first, and then look for unexpected input or output), exercise, thinking ability, and the efficiency of code execution is more sensitive to the () reduce the time complexity.

How to use the actual project?

The following example I gave can be well reflected: the use of recursive algorithms and improve the efficiency of execution.

In common business systems, dynamic menus and permissions are inevitably required. A dynamic menu is essentially a tree, and the permissions under a menu route are like leaf nodes in the tree. But databases can’t store structures like trees, so what?

The routing menu in the normal tree structure is as follows:

The database table designed is as follows:

ParentId: indicates the Id of the parent node. MenuType: Indicates the permission of the route when the type is C, that is, the leaf node in the binary treeCopy the code

The actual business needs to turn the found list into a tree structure and return it to the front end. How does this work? The general idea is as follows:

    /** * list to tree */
    private List<MenuBO> list2Tree(List<MenuBO> list, Integer pId) {
        List<MenuBO> tree = new ArrayList<>();
        Iterator<MenuBO> it = list.iterator();
        while (it.hasNext()) {
            MenuBO m = it.next();
            if (m.getParentId() == pId) {
                tree.add(m);
                // The added element is deletedit.remove(); }}// Find the child element
        tree.forEach(n -> n.setChildren(list2Tree(list, n.getId())));
        return tree;
    }
Copy the code

Find all the elements with the same PID in the original list and place them in the new list, and set the new list as child nodes until all nodes are iterated. And because a row becomes only one node in the tree, each element needs to be traversed only once. So when you add an element to the new list, you remove it from the original list. The algorithm complexity changes from O(N*logN) to 0(N), which greatly improves the execution efficiency.

conclusion

At this time last year, I started to brush questions with zero basis. It took me one or two hours to write a medium difficulty question at the beginning, and then I had to output an article after writing the question. Generally, it took me until about midnight. But now I have been able to hit the easy problems, medium difficulty problems have less time to solve the problem, as for the difficult problems still need to see the solution.

From my experience of more than a year, I can sum up two sentences:

  1. All things are difficult before they are easy
  2. Practice is the sole criterion for testing truth!

So take action! Just now!

Finally, I want to thank the rare earth nuggets! Not only do I get to write boiling points, I get to write articles and win prizes. I have to say, the Nuggets have a lot of activities (PS: lots of prizes)!