How to debug LeetCode code in VS Code?

Time:2020-1-21

Abstract:Interview brush guide.

  • How to debug LeetCode code in VS Code
  • By Neo CS

Fundebug is reprinted by authorization, and the copyright belongs to the original author.

Recently, many small partners have been asked for help. I want to know how to debug LeetCode code in VS Code. Generally speaking, in order to debug the local code, we need to install the relevant language support plug-ins. In this article, we will take debugging leetcode java code as an example to introduce common routines of local debugging leetcode code.

To learn how to brush questions in vs code, you can move to leetcode for vs Code: Spring recruit offer harvester.

Preparation

First, make sure that JDK is installed in the system. There are many related tutorials, which will not be covered here.

Then we need to make sure that the following plug-ins are installed in vs Code:

1. LeetCode

Used to generate questions and submit answers.

2. Language Support for Java(TM) by Red Hat

Provide intelligent prompt and other language related functions.

3. Debugger for Java,Java

Debugger.

After installation, you can see the three plug-ins in the plug-in management column of vs Code:

How to debug LeetCode code in VS Code?

If the vs Code prompts that the JDK cannot be found after opening the java file, please check whether the relevant configuration is correct.

Write debugging code:

Let’s take question 20: valid brackets as an example.

During the answer process, you may see some red lines in the editor. Don’t worry, it shows that the language support for Java plug-in is working. Usually, this means that your code has syntax errors. The following example shows the error cause because the dependency package is used but not imported into the current file. We can use quick fix to fix:

How to debug LeetCode code in VS Code?

When importing dependent packages, it is necessary to ensure that the files can be compiled correctly. When leetcode checks the answer, it does not require the corresponding import statement to exist in the file, so the existence of import statement will not affect the final check result.

After writing the answer, we need to add a main function as the execution entry of the debugger in the same file. The code structure of the whole file is as follows:

class Main {
    public static void main(String[] args) {
        // Create a new Solution instance
        Solution solution = new Solution();
        // Create a test case
        String testCase = "()[]{}";
        // Get the answer
        boolean answer = solution.isValid(testCase);
        // Print the answer
        System.out.println(answer);
    }
}

class Solution {

    ...

    public boolean isValid(String s) {

        ...

        return answer;
    }
}

At this point, we will see two codelens buttons appear above the main function:

How to debug LeetCode code in VS Code?

Click the run button to run the main function. We can see the output of the program in the debug console that pops up below (because we used println to output the answer in the last line of code).

If you want to debug, you can set the breakpoint at the corresponding line number position, and click the debug button to enter the debugging mode to check the code operation.

One thing to note here is that since the class names of the answer templates generated by leetcode are all solutions, there will be multiple classes with the same name in the same directory, which may cause the code to not execute correctly. Therefore, if you want to debug leetcode java code, but there are multiple leetcode java files in the current directory, you need to ensure the uniqueness of the class names , we can change the name of the solution class to be debugged (but remember to change the name back when submitting), or simply copy it to another clean directory for debugging.

The above is how to debug LeetCode Java code in VS Code. For other languages, it is basically a very similar step. If you have better suggestions or have your favorite debugging skills, please leave a message in the comment area.