- This post was originally posted on my personal blog:”Programmer charging Station”
- Article links:”Portal”
1. What is LeetCode
“LeetCode” is an Online code evaluation platform (Online Judge), including algorithm, database, Shell, multithreading and other different categories of topics, among which algorithm is the main topic. We can practice our programming skills and improve our algorithmic abilities by solving problems in the LeetCode question bank.
LeetCode has 2,300 + programming questions, supports 16+ programming languages (C, C++, Java, Python, etc.), and has an active community for sharing technical topics, professional experiences, topic exchanges, and more.
And many well-known Internet companies like to examine LeetCode questions in the interview, usually in the form of handwritten code. Interviewers are required to analyze and give solutions to given problems, and sometimes they are required to analyze the time and space complexity of algorithms, as well as the idea of algorithms. Interviewers determine the level of thinking ability to solve problems by examining the interviewees’ familiarity with common algorithms and their ability to implement them.
No matter in the interview with well-known Domestic or foreign Internet companies, it is helpful to get a good offer from a good company to brush the questions through LeetCode and fully prepare the algorithm.
2. LeetCode beginner
2.1 LeetCode registered
- Open the Chinese homepage of LeetCode, link: LeetCode official website.
- Enter the mobile phone number to obtain the verification code.
- After entering the verification code, click “Log in/Register” and you are registered.
2.2 LeetCode question bank
“Question bank” is the most direct entry to practice in LeetCode, where you can brush questions according to the label, difficulty and status of the questions. You can also click “random question” to start brushing questions.
2.2.1 Topic labels
LeetCode’s topic covers a number of algorithms and data structures. Greedy, search, dynamic programming, linked list, binary tree, hash table and so on, you can select the corresponding label for special brush, but also can see the completion of the corresponding topic.
2.2.2 List of topics
LeetCode provides search filtering for topics. You can filter related lists, different levels of difficulty, topic completion status, different labels of the topic. It can also be sorted by question number, number of questions solved, pass rate, difficulty, frequency of occurrence, etc.
2.2.3 Current Progress
Current progress provides a visual representation of progress. You can see your own practice overview here. The progress will automatically show you how you are doing. You can also click “Progress Setting” to create a new progress, where you can also modify or delete the relevant progress.
2.2.4 Subject details
Click on the topic link to see the topic’s description and code editor. Here you can also view the relevant problem solutions and their own submission records.
2.3 LeetCode brush language
Big factories in the interview algorithm is to examine the basic skills, what language there is no limit, will not affect the results. It is recommended to use a language you are familiar with, or a language with simple grammar.
Compared with Java and Python, the syntax related to C and C++ is more complex. On the one hand, you need to think about ideas when doing problems, and on the other hand, you need to study the syntax. In addition, complicated grammar is not conducive to understanding ideas, consuming more time, which is not conducive to the efficiency of brushing questions. During the interview, we often need to complete as many questions as possible within an hour. Once C++ grammar mistakes, it is easy to panic. Of course, the LeetCode week champions are more likely to brush in C++, because it has become a tradition to compete in algorithms in C++, and most OI/ACM contestants are C++ champions.
From my personal experience, I used C, C++ and a little Java when I participated in ACM competitions in college. For better efficiency, I chose Python. I feel that by using Python, I can focus more on the algorithm and the data structure itself, and I can achieve faster efficiency.
Personal advice: Life is short, I use Python.
2.4 LeetCode brush question process
In “2.2 LeetCode Question Bank — 2.2.4 Question Details”, we introduce the relevant information about the questions.
You can see the topic description area on the left, where you can see the topic description and some sample data. On the right is the code editing area, which by default shows the methods to be implemented.
We need to implement the corresponding algorithm based on the parameters given to the method in the code editor and return the results that the problem requires. After that, it should pass the test result of “executing code”. After clicking “Submit”, the execution result will be displayed as “passing”, and a topic will be considered complete.
To sum up our brushing process:
- Select a problem you want to solve from the LeetCode question bank.
- Look at the description on the left of the question to understand the requirements.
- Think about the solution and implement the corresponding method in the code editing area on the right and return the result required by the problem.
- If you really can’t come up with a solution, look at the solution to the problem and try to understand the other person’s solution and code.
- Click the “Execute Code” button to test the results.
- If the output doesn’t match the expected results, go back to Step 3 to rethink the solution and rewrite the code.
- If the output is as expected, click the submit button.
- If the execution result shows “compilation error”, “solution error”, “execution error”, “time limit exceeded”, “memory limit exceeded”, etc., you need to go back to Step 3 to rethink the solution, or think about special data, and rewrite the code.
- If the result says “yes”, congratulations on passing the problem.
Next, we will explain how to brush questions in LeetCode through the topic “1. Sum of two numbers – Force button (LeetCode)”.
2.5 LeetCode problem 1
2.5.1 Topic Links
- 1. Sum of two numbers – LeetCode
2.5.2 Main idea of the topic
-
Description: Given an integer array nums and an integer target value target.
-
Find two integers in this array with and as target and print the subscripts of these two integers.
2.5.3 Solution idea
- Idea 1: violent search
The simplest way to think about it is to iterate through the array, looking for target-nums [I] for each nums[I] in the array.
However, this method makes use of the two-fold cyclic violent search, and the time complexity is O(n2)O(n^2)O(n2). Although it can solve the problem, there is a better solution.
- Idea 2: Hash table
Another way to think about it is to use hash tables. The key-value pairs in the dictionary are target-nums[I] : I. I is the subscript.
If target-nums [I] exists in the dictionary, the index corresponding to target-nums [I] and the index I of the current array are printed. If not, store the subscript I of target-nums[I] in the dictionary.
2.5.4 Solution code
- Idea 1: violent search
class Solution:
def twoSum(self, nums: List[int], target: int) - >List[int] :
size = len(nums)
for i in range(size):
for j in range(i + 1, size):
if nums[i] + nums[j] == target:
return [i, j]
return []
Copy the code
- Idea 2: Hash table
class Solution:
def twoSum(self, nums: List[int], target: int) - >List[int] :
size = len(nums)
num_dict = dict(a)for i in range(size):
if target - nums[i] in num_dict:
return [num_dict[target - nums[i]], i]
num_dict[nums[i]] = i
return []
Copy the code
Once you understand the above question, try writing your own code and try to submit it. You can also write the code after understanding my problem solving ideas and code, and try to submit it.
If the submission result says “yes”, congratulations on completing question 1 on LeetCode. Although it is only one question, but it means the beginning of the brush question plan! I hope you can stick to it and get what you deserve.
3. LeetCode brush problem guide
3.1 Preparation for LeetCode
It’s a good idea to have some basic knowledge of the “data structure” and “algorithm” classes before you swipe LeetCode. These foundations include:
- Common data structures: arrays, strings, linked lists, trees (such as binary trees), etc.
- Often test algorithms: divide and conquer algorithm, greedy algorithm, exhaustive algorithm, backtracking algorithm, dynamic programming and so on.
You can also learn with relevant books and materials:
- “Algorithms (4th Edition) – Translated by Xie Luyun”
- 【 Book 】 “Big Talk Data Structure – By Cheng Jie”
- 【 Book 】 “Fun Algorithms – By Chen Xiaoyu”
- [Book] “Algorithms illustrated by Yuen Kwok-chung”
- 【 Book 】 “Introduction to Algorithm Competition Classic (2nd Edition) – By Liu Rujia”
- Data Structure and Algorithm Analysis – Translated by Feng Shunxi
- Introduction to Algorithms (3rd edition) by Yin Jianping/Xu Yun/Wang Gang/Liu Xiaoguang/Su Ming/Zou Hengming/Wang Hongzhi
3.2 Order of LeetCode brush questions
As a joke, there was once a guy who thought LeetCode’s problems were sorted by difficulty, so he started with “1. Sum of two numbers” and got stuck on “4. Find the median of two positive ordinal groups”.
LeetCode’s questions are not numbered in order of difficulty, so it is not recommended to swipe them in order unless you are hardcore. If you are a novice, it is recommended to start from the “easy” level of the algorithm to brush questions. After you get familiar with the simple questions, then start to brush the medium difficulty questions according to the label categories. Once you’re done with the medium difficulty questions, think about the interview questions or difficult questions.
In fact, the Official LeetCode website has a list of well-organized brush questions. The link is: “LeetBook – Force button”. You can start by brushing the title card here. I’ve also done a little tidbit here.
The recommended sequence and table of contents are as follows:
- The primary algorithms
- Array class algorithm
- Arrays and Strings
- Linked list algorithms
- Hash table
- Queue & stack
- recursive
- Binary search
- Binary tree
- Intermediate algorithm
- Advanced algorithms
- Algorithmic interview question summary
Of course, you can also use the official new “data structure” – learning plan, “algorithm” – learning plan to brush every day. Or brush the questions according to the list of brush questions I summarized (to be written).
3.3 LeetCode brush skills
3.3.1 “5-Minute Thinking”
“5-minute thinking method” means: if you have an idea for a problem within 5 minutes, start to solve it immediately. If you don’t have an idea after 5 minutes, go straight to the solution. Then according to the solution of the idea, to achieve their own code. If you find yourself unable to implement the code, read it carefully and understand the logic of the code.
In fact, it is similar to the process of memorizing words in English.
In the beginning, learn simple words first, master the basic vocabulary, then learn phrases, learn sentences, and then read the article. Moreover, when reciting words, it is not just reciting them again. It’s about repetition.
Algorithm brush problem is the same, zero basis brush problem, do not tangle too much how can not come up with their own algorithm solution, how can not think of a more efficient method. When you learn English, you start with the first letter.
In the beginning, if you don’t know how to solve the problem, look at the solution, as quickly as possible.
3.3.2 “Repeated Brush questions”
Algorithm problems sometimes brush over the past, over a long time may forget, see the problem done before can not immediately think of the solution idea. In fact, this is the same as memorizing words, words are not completely remembered after a look. So a brush is not the end of the topic, but also need to constantly review.
Moreover, there may be multiple solutions to a problem, and there may be less complex algorithmic ideas.
When you do it at first, it might be one idea, and when you do it a second time, you might come up with a new solution, a new optimization, and so on.
Therefore, the algorithm problem after a meeting will not, but also can brush a few times, continue to deepen understanding.
3.3.3 “Write problem Solving Report”
Brush algorithm problems, there is a very useful shortcut, is “write solution report”. If you can brush a problem, the steps to solve the problem, the idea of doing the problem in easy-to-understand words to write the problem report, then the problem is considered to have mastered. In fact, it is equivalent to “Feynman method of learning” thinking. In this way, you can also reduce the number of times to brush the problem. If you encounter a problem that you have brushed before, but have no idea for a while, you can look at your previous problem solving report. This saves a lot of time repeating questions.
The resources
- 【 article 】What is LeetCode? – Quora
- LeetCode Help Center – LeetCode
- Is leetcode used in python or c++? – zhihu
- What is the level of LeetCode? What level of offer can you get? – zhihu