Summary: Interview brush questions guide.

  • How to debug LeetCode in VS Code
  • Author: neo – cs

FundebugReproduced with authorization, copyright belongs to the original author.

I’ve recently received a lot of help from people who want to know how to debug LeetCode in VS Code. In general, to debug native code, we need to install the relevant language support plug-ins. In this article, we take debugging LeetCode Java code as an example, to introduce you to local debugging LeetCode code common routines.

If you want to learn how to get ahead in VS Code, you can go to LeetCode for VS Code.

The preparatory work

First, make sure you have the JDK installed on your system. There are many tutorials that will not be covered here.

Then we need to make sure 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 prompts and other language-related functions.

3. Debugger for Java

The debugger.

After the installation is complete, VS Code’s plugins management bar will display these three plugins:

If VS Code tells you that the JDK cannot be found after opening the Java file, check that the configuration is correct.

Write debug code:

Let’s take number 20: valid parentheses as an example.

As you answer, you may see some red lines in the editor. Don’t worry, this shows that the Language Support for Java plug-in is working. This usually means that your code has a syntax error, which the following example shows is caused by using dependency packages but not importing them into the current file. We can Fix this using the Quick Fix feature:

Importing dependencies ensures that files are compiled correctly. LeetCode does not require the presence of an import statement in the file when checking the answer, so the presence or absence of an import statement does not affect the final check result.

After writing the answer, we also need to add a Main function in the same file as the debugger execution entry, the whole file code structure 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 = "[the] {} ()";
        // Get the answer
        boolean answer = solution.isValid(testCase);
        // Print the answerSystem.out.println(answer); }}class Solution {...public boolean isValid(String s) {...returnanswer; }}Copy the code

At this point we see two CodeLens buttons appearing above the Main function:

Clicking the Run button will Run the Main function, and you can see the program’s output in the Debug Console that pops up below (because we used println to print the answer in the last line of code).

If you want to Debug, you can set a breakpoint at the appropriate line number and click the Debug button to go into Debug mode to see how the code is running.

One thing to note here is that since the class name of the answer template generated by LeetCode is Solution, there will be multiple classes with the same name in the same directory, which may lead to the code cannot be executed correctly. Therefore, if you want to debug LeetCode Java code, However, if there are multiple LeetCode Java files in the current directory, we need to ensure the uniqueness of the class name. We can change the name of the Solution class being debugged (but remember to change the name back when submitting), or simply copy it to another clean directory for debugging.

That’s how to debug LeetCode Java Code in VS Code, and it’s pretty much the same for any other language. If you have any suggestions or favorite debugging tips, feel free to leave them in the comments!