简体中文 | English
<img align="right" height=150 src="https://github.com/j178/leetgo/assets/10510431/4dbe3a58-6c64-4b54-a695-78c7d8c8de39" />
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 <us or cn> -l <lang>leetgo.yamlleetgo pick <id or name or today>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:
<!-- BEGIN 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 j178/tap/leetgo
scoop bucket add j178 https://github.com/j178/scoop-bucket.git scoop install j178/leetgo
yay -S leetgo-bin
curl -fsSL https://raw.githubusercontent.com/j178/leetgo/master/scripts/install.sh | bash
go installgo install github.com/j178/leetgo@latest
Usage:
leetgo [command]
Available Commands:
init Init a leetcode workspace
pick Generate a new question
info Show question info
test Run question test cases
submit Submit solution
fix Use ChatGPT API to fix your solution code (just for fun)
edit Open solution in editor
contest Generate contest questions
cache Manage local questions cache
debug Show debug info
open Open one or multiple question pages in a browser
help Help about any command
Flags:
-v, --version version for leetgo
-l, --lang string language of code to generate: cpp, go, python ...
--site string leetcode site: cn, us
-y, --yes answer yes to all prompts
-h, --help help for leetgo
Use "leetgo [command] --help" for more information about a command.
<!-- END USAGE -->
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):
leetgo pick two-sum # `two-sum` is the question slug leetgo pick 1 # `1` is the question id leetgo pick today # `today` means daily question leetgo pick yesterday # `yesterday` means the question of yesterday leetgo pick today-1 # `today-1` means the question of yesterday, same as `yesterday`. `today-2`, `today-3` etc are also supported. leetgo contest weekly100 # `weekly100` means the 100th weekly contest leetgo test last # `last` means the last generated question leetgo test weekly100/1 # `weekly100/1` means the first question of the 100th weekly contest leetgo submit b100/2 # `b100/2` means the second question of the 100th biweekly contest leetgo submit w99/ # `w99/` means all questions of the 99th biweekly contest (must keep the trailing slash) leetgo test last/1 # `last/1` means the first question of the last generated contest leetgo test last/ # `last/` means all questions of the last generated contest (must keep the trailing slash)
[!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:
<details> <summary>Click to expand</summary> <!-- BEGIN CONFIG --><!-- END CONFIG --> </details># Your name author: Bob # Language of the question description: 'zh' (Simplified Chinese) or 'en' (English). language: zh code: # Language of code generated for questions: go, cpp, python, java... # (will be overridden by command line flag -l/--lang). lang: go # The default template to generate filename (without extension), e.g. {{.Id}}.{{.Slug}} # Available attributes: Id, Slug, Title, Difficulty, Lang, SlugIsMeaningful # (Most questions have descriptive slugs, but some consist of random characters. The SlugIsMeaningful boolean indicates whether a slug is meaningful.) # Available functions: lower, upper, trim, padWithZero, toUnderscore, group. filename_template: '{{ .Id | padWithZero 4 }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}' # Generate question description into a separate question.md file, otherwise it will be embed in the code file. separate_description_file: true # Default modifiers for all languages. modifiers: - name: removeUselessComments go: # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp. out_dir: go # Functions that modify the generated code. modifiers: - name: removeUselessComments - name: changeReceiverName - name: addNamedReturn - name: addMod python3: # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp. out_dir: python # Path to the python executable that creates the venv. executable: python3 cpp: # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp. out_dir: cpp # C++ compiler cxx: g++ # C++ compiler flags (our Leetcode I/O library implementation requires C++17). cxxflags: -O2 -std=c++17 rust: # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp. out_dir: rust java: # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp. out_dir: java leetcode: # LeetCode site, https://leetcode.com or https://leetcode.cn site: https://leetcode.cn # Credentials to access LeetCode. credentials: # How to provide credentials: browser, cookies, password or none. from: browser # Browsers to get cookies from: chrome, safari, edge or firefox. If empty, all browsers will be tried. Only used when 'from' is 'browser'. browsers: [] contest: # Base directory to put generated contest questions. out_dir: contest # Template to generate filename of the question. filename_template: '{{ .ContestShortSlug }}/{{ .Id }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}' # Open the contest page in browser after generating. open_in_browser: true # Editor settings to open generated files. editor: # Use a predefined editor: vim, vscode, goland # Set to 'none' to disable, set to 'custom' to provide your own command and args. use: none # Custom command to open files. command: "" # Arguments to your custom command. # String contains {{.CodeFile}}, {{.TestFile}}, {{.DescriptionFile}}, {{.TestCasesFile}} will be replaced with corresponding file path. # {{.Folder}} will be substituted with the output directory. # {{.Files}} will be substituted with the list of all file paths. args: ""
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, Safari1, Edge.
leetcode: credentials: from: browser
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: cookies
Provide username and password through LEETCODE_USERNAME and LEETCODE_PASSWORD environment variables.
leetcode: credentials: from: password
[!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 <iostream> 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.
For Safari on macOS, you may need to grant Full Disk Access privilege to your terminal app which you would like to run ↩


阿里Qoder团队推出的桌面端AI智能体
QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。


全球首个AI音乐社区
音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。


一站式搞定所有学习需求
不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。


为AI短剧协作而生
专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创 意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。


能听懂你表达的视频模型
Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。


国内直接访问,限时3折
输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动


职场AI,就用扣子
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!


多风格AI绘画神器
堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。


零代码AI应用开发平台
零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号