leetcode-local-runner
v1.0.3
Published
Auto-run LeetCode Java solutions locally without modifying Solution.java
Maintainers
Readme
LeetCode Local Runner
A cross-platform CLI tool to automatically compile and run LeetCode Java solutions locally without modifying your Solution.java file.
Write code like you do on LeetCode → get instant results on your machine.
Features
- File Watching: Automatically detects changes in any
Solution.javafile. - Code Generation: Automatically generates a runnable
Main.javafile. - Smart Parsing: Detects the solution method name and its parameters.
- Classpath-Free: Embeds your
Solutionclass into aMainclass to avoid classpath issues. - Helper Classes: Automatically includes common helper classes like
ListNodeandTreeNode. - Cross-Platform: Works on Windows, Ubuntu, and macOS.
- Stable Polling: Uses a reliable polling mechanism to watch for file changes.
Installation
You can install the tool globally via npm, which is the recommended approach.
npm install -g leetcode-local-runnerAlternatively, you can install it as a development dependency in your project:
npm install --save-dev leetcode-local-runnerUsage
Open your terminal or command prompt.
Navigate to the directory containing your LeetCode problem folders (each with a
Solution.java).Run the following command:
leetcode-watchThe tool will start watching for file modifications. Every time you save a
Solution.javafile, it will automatically:- Generate a new
Main.javain the same directory. - Compile and run
Main.javato show you the output.
- Generate a new
Now you can focus on writing your solution and see the results instantly!
Why This Tool Exists
The official VS Code LeetCode extension, with over 1.2 million downloads, is excellent for:
- Browsing problems
- Writing solutions
- Submitting to LeetCode
But it does not provide a stable local Java runner without:
- Editing
Solution.java - Writing custom
main()methods - Fighting classpath / package issues
LeetCode Local Runner solves exactly that, while staying 100% compatible with the extension.
Required VS Code Configuration
To ensure zero conflicts between the extension and this runner, add the following to your settings.json:
{
"leetcode.hint.configWebviewMarkdown": false,
"editor.inlineSuggest.edits.allowCodeShifting": "never",
"leetcode.hint.commentDescription": false,
"leetcode.hint.commandShortcut": false,
"leetcode.filePath": {
"default": {
"folder": "${id}.${name}",
"filename": "Solution.${ext}"
}
}
}Why These Settings Matter
| Setting | Reason |
| :--- | :--- |
| Solution.java filename | Required for automatic detection |
| Stable folder naming | Ensures watcher works correctly |
| Disable inline hints | Prevents file rewrite / cursor shifts |
| No markdown hints | Avoids unintended code injection |
With this configuration, both tools coexist perfectly.
How It Works
The script works by parsing your Solution.java file to understand its structure. It then generates a complete, runnable Main.java file that wraps your code.
For example, if your Solution.java is:
// Solution.java
class Solution {
public int[] twoSum(int[] nums, int target) {
// Your code here...
}
}The tool will generate a Main.java file like this:
// Main.java (Auto-generated)
import java.util.*;
public class Main {
// Helper classes like ListNode or TreeNode are automatically injected here if needed.
static class Solution {
// Your entire Solution.java code is placed here.
public int[] twoSum(int[] nums, int target) {
// Your code here...
}
}
public static void main(String[] args) {
Solution sol = new Solution();
// Dummy data is used to call your method.
// (Note: The dummy data is hardcoded in the script for now)
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = sol.twoSum(nums, target);
System.out.println(Arrays.toString(result));
}
}This design means you don't have to worry about managing classpaths or writing a main method just for testing.
Contributing
Contributions are welcome! Please feel free to open a Pull Request or an Issue to discuss any changes.
License
This project is licensed under the MIT License.
