Best LeetCode friend for geek. :snowboarder:
简体中文 | English
leetgo is a command-line tool for LeetCode that provides almost all the functionality of LeetCode,
allowing you to do all of your LeetCode exercises without leaving the terminal.
It can automatically generate skeleton code and test cases, support local testing and debugging,
and you can use any IDE you like to solve problems.
And leetgo also supports real-time generation of contest questions, submitting all questions at once, so your submissions are always one step ahead!
leetgo init -t -l leetgo.yamlleetgo pick leetgo test last -Lleetgo submit lastYou can test and submit in one command: leetgo test last -L -s
You can edit the question file in your favorite editor: leetgo edit last
leetgo supports code generation for most languages, and local testing for some languages.
In the Go language, running leetgo pick 257 will generate the following code:
// Omitted some code...
// @lc code=begin
func binaryTreePaths(root *TreeNode) (ans []string) {
return
}
// @lc code=end
func main() {
stdin := bufio.NewReader(os.Stdin)
root := Deserialize[*TreeNode](ReadLine(stdin))
ans := binaryTreePaths(root)
fmt.Println("output: " + Serialize(ans))
}This is a complete and runnable program. You can run it directly, input the test cases, and compare the results.
leetgo test -L will automatically run this program with the test cases in testcases.txt and compare the results.
Local testing means that you can run the test cases on your local machine, so you can use a debugger to debug your code.
Local testing requires more work to implement for each language, so not all languages are supported. Below is the current support matrix:
| Generation | Local testing | |
|---|---|---|
| Go | :white_check_mark: | :white_check_mark: |
| Python | :white_check_mark: | :white_check_mark: |
| C++ | :white_check_mark: | :white_check_mark: |
| Rust | :white_check_mark: | :white_check_mark: |
| Java | :white_check_mark: | Not yet |
| JavaScript | :white_check_mark: | Not yet |
| TypeScript | :white_check_mark: | Not yet |
| PHP | :white_check_mark: | Not yet |
| C | :white_check_mark: | Not yet |
| C# | :white_check_mark: | Not yet |
| Ruby | :white_check_mark: | Not yet |
| Swift | :white_check_mark: | Not yet |
| Kotlin | :white_check_mark: | Not yet |
| Bash | :white_check_mark: | Not yet |
| MySQL | :white_check_mark: | Not yet |
| MSSQL | :white_check_mark: | Not yet |
| Oracle | :white_check_mark: | Not yet |
| Erlang | :white_check_mark: | Not yet |
| Racket | :white_check_mark: | Not yet |
| Scala | :white_check_mark: | Not yet |
| Elixir | :white_check_mark: | Not yet |
| Dart | :white_check_mark: | Not yet |
Welcome to help us implement local testing for more languages!
You can download the latest binary from the release page.
brew install leetgoscoop bucket add j178 https://github.com/j178/scoop-bucket.git
scoop install j178/leetgoyay -S leetgo-bincurl -fsSL https://raw.githubusercontent.com/j178/leetgo/master/scripts/install.sh | bashgo installgo install github.com/j178/leetgo@latest…Many leetgo commands rely on qid to find the leetcode question. qid is a simplified question
identifier defined by leetgo, which includes the following forms (using the two-sum problem as an example):
…[!WARNING] Since
v1.4,leetgono longer reads the global~/.config/leetgo/config.yamlfile, please put all configurations in the project'sleetgo.yamlfile.
leetgo init generates a leetgo.yaml file in the current directory, which contains all the configurations of leetgo. You can modify this file according to your needs.
The directory where leetgo.yaml is located is considered as the root directory of a leetgo project, and leetgo will generate all code files undeer this directory. leetgo will look for the leetgo.yaml file in the current directory. If it is not found, it will recursively search upwards until a leetgo.yaml file is found or the root directory of the file system is reached.
Below is the demonstration of a complete configuration:
Click to expand
…leetgo uses LeetCode's GraphQL API to retrieve questions and submit solutions. leetgo needs your LeetCode cookies to access the authenticated API.
There are three ways to make cookies available to leetgo:
Read cookies from browser automatically.
Currently, leetgo supports Chrome, FireFox, Safari[^1], Edge.
leetcode:
credentials:
from: browser[!IMPORTANT]
On Windows, Chrome/Edge v127 enabled App-Bound Encryption andleetgocan no longer decrypt cookies from Chrome/Edge. You would need to provide cookies manually or use other browsers.
Provide cookies.
You can get your cookies named LEETCODE_SESSION and csrftoken from browser's developer tools, and set the LEETCODE_SESSION and LEETCODE_CSRFTOKEN environment variables. If you are using leetcode.com, LEETCODE_CFCLEARANCE should also be set to the value of the cf_clearance cookie.
leetcode:
credentials:
from: cookiesProvide username and password through LEETCODE_USERNAME and LEETCODE_PASSWORD environment variables.
leetcode:
credentials:
from: password[!TIP] You can specify which browser to read cookies from, e.g.
browsers: [chrome].
You can specify multiple authentication methods,leetgowill try them in order, e.g.from: [browser, cookies].
You can put all the environment variables in a.envfile in the project's root directory,leetgowill automatically read them.
[!NOTE] Password authentication is not recommended, and it is not supported by
leetcode.com.
You can put environment variables in a .env file in the project's root directory, and leetgo will automatically read them.
testcases.txttestcasts.txt is generated by leetgo and contains all the test cases of the question.
You can add a new test case by specifying only the input and leaving the output blank. When you run leetgo test (without -L), the expected output will be retrieved from the remote server.
For example:
input:
[3,3]
6
output:
input:
[1,2,3,4]
7
output:Several fields in leetgo's config file support templating. These fields are often suffixed with _template.
You can use custom template to generate your own filename, code, etc.
A code file is composed of different blocks, you can overwrite some of them to provide your own snippets.
| Available blocks |
|---|
| header |
| description |
| title |
| beforeMarker |
| beforeCode |
| code |
| afterCode |
| afterMarker |
For example:
code:
lang: cpp
cpp:
blocks:
- name: beforeCode
template: |
#include
using namespace std;
- name: afterMarker
template: |
int main() {}leetgo supports providing a JavaScript function to handle the code before generation, for example:
code:
lang: cpp
cpp:
modifiers:
- name: removeUselessComments
- script: |
function modify(code) {
return "// hello world\n" + code;
}If you encounter any problems, please run your command with the DEBUG environment variable set to 1, copy the command output, and open an issue.
Some common problems can be found in the Q&A page.
Good First Issues are a good place to start, and you can also check out some Help Wanted issues.
If you want to add local testing support for a new language, please refer to #112.
Before submitting a PR, please run golangci-lint run --fix to fix lint errors.
Here are some awesome projects that inspired me to create this project:
Also thanks to JetBrains for providing free licenses to support this project.
[^1]: For Safari on macOS, you may need to grant Full Disk Access privilege to your terminal app which you would like to run leetgo.
No open issues yet, or sync has not completed.