本地调试 LeetCode、自动生成每日题目、直接提交解决方案等更多功能!
Benhao 的 LeetCode 算法题解
克隆仓库后,添加 .env 文件来指定题目和解决方案的位置(本地)。
对于远程 GitHub Action,需要添加 COOKIE(LeetCode cookie)、PUSH_KEY(PushDeer 通知)、PROBLEM_FOLDER(题目存放位置)、LEETCODE_USER(LeetCode 个人主页 uri)、LOG_LEVEL(日志级别)。
注意: 如果需要使用 python3 以外的语言,在 .env 中添加 LANGUAGES="python3,golang" 等
.env 文件示例:
PROBLEM_FOLDER="problems"
PUSH_KEY="***[PushDeer 的 key]"
COOKIE="***[LeetCode graphql 的 cookie]"
LANGUAGES="python3,golang,java,cpp,typescript,rust"
LEETCODE_USER="himymben"
LOG_LEVEL="info"
PYTHONPATH=.
当两道题目解法相同但数据范围不同时(例如 n <= 100 vs n <= 10^5),可以启用自动链接功能避免重复代码:
AUTO_LINK_SIMILAR="true" # 设置为 "true" 启用(默认关闭)
启用后,系统会通过比较以下内容检测相似题目:
- 题目描述(归一化处理)
- 方法签名(方法名、参数、返回类型)
- 标题相似度
如果发现相似题目,只创建 link.json 而不创建解答文件:
{
"link_to": "3740",
"link_folder": "problems",
"reason": "Auto-detected: description matches, method_name matches, signature matches, different constraints"
}对于 GitHub Actions,在仓库设置中添加 AUTO_LINK_SIMILAR secret 并设为 true。
安装 python3.14 或更高版本的要求:
pip install -r python/requirements.txtLeetCode 工具集:
python python/scripts/leetcode.py使用示例:
Setting up the environment...
Please select the configuration [0-1, default: 0]:
0. Load default config from .env
1. Custom config
1
Select multiple languages you want to use, separated by comma [0-5, default: 0]:
0. python3
1. java
2. golang
3. cpp
4. typescript
5. rust
0,2
Languages selected: python3, golang
自动从浏览器获取 LeetCode CN Cookie,支持更新 GitHub Secrets 或本地 .env 文件。
# 只更新 GitHub Secrets
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode
# 只更新本地 .env
python python/scripts/leetcode_cookie_updater.py --env .env
# 同时更新
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode --env .env
# 开启 debug 日志
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode --log-level DEBUG
# 指定 GitHub Token
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode --github-token ghp_xxx| 参数 | 说明 |
|---|---|
--repo REPO |
GitHub 仓库名 (如 QuBenhao/LeetCode),不指定则不更新 GitHub |
--env PATH |
本地 .env 文件路径,不指定则不更新本地文件 |
--log-level LEVEL |
日志级别: DEBUG, INFO, WARNING, ERROR (默认: INFO) |
--github-token TOKEN |
GitHub Token (也可通过环境变量 GITHUB_TOKEN 设置) |
如果要更新 GitHub Secrets,需要创建具有以下权限的 Token:
repo(完整仓库访问)workflow(更新 GitHub Actions)secret(更新GitHub仓库的Secret)
创建步骤:
- 访问 https://github.com/settings/tokens
- 点击 "Generate new token (classic)"
- 勾选
repo和workflow和secret权限 - 生成并保存 Token
可以通过 crontab 设置定时任务自动更新 Cookie:
# 每天凌晨 2 点更新
0 2 * * * GITHUB_TOKEN="ghp_xxx" python /path/to/leetcode_cookie_updater.py --repo QuBenhao/LeetCode >> /tmp/leetcode_cookie.log 2>&1- Chrome
- Edge
- Firefox
- Chromium
查看 Java README
查看 Cpp README
查看 Rust README
克隆你 fork 的仓库
注意:创建你自己的分支并设为默认分支,保留 master 分支!
打开代码项目并安装所需的语言环境。
创建你自己的 .env 文件(注意:最好使用与作者不同的题目文件夹,会有很多冲突):
PROBLEM_FOLDER=demo
COOKIE="***[LeetCode graphql 的 cookie]"
LANGUAGES="golang,java"
根据你的 .env 创建 'demo' 文件夹
运行脚本获取题目、运行测试并提交你的解决方案。
如果遇到如下问题,
它会添加题目并修改你使用语言的测试文件:

在 VsCode 中,
在 .vscode 下添加 launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Typescript Test",
"type": "node",
"request": "launch",
"preLaunchTask": "typescript-test",
},
{
"name": "Typescript Tests",
"type": "node",
"request": "launch",
"preLaunchTask": "typescript-tests",
},
{
"name": "Python Test",
"type": "node",
"request": "launch",
"preLaunchTask": "python-test",
},
{
"name": "Python Tests",
"type": "node",
"request": "launch",
"preLaunchTask": "python-tests",
},
{
"name": "Golang Test",
"type": "node",
"request": "launch",
"preLaunchTask": "golang-test",
},
{
"name": "Golang Tests",
"type": "node",
"request": "launch",
"preLaunchTask": "golang-tests",
},
{
"name": "C++ Test",
"type": "node",
"request": "launch",
"preLaunchTask": "cpp-test",
},
{
"name": "C++ Tests",
"type": "node",
"request": "launch",
"preLaunchTask": "cpp-tests",
},
{
"name": "Java Test",
"type": "node",
"request": "launch",
"preLaunchTask": "java-test",
},
{
"name": "Java Tests",
"type": "node",
"request": "launch",
"preLaunchTask": "java-tests",
},
{
"name": "Rust Test",
"type": "node",
"request": "launch",
"preLaunchTask": "rust-test",
},
{
"name": "Rust Tests",
"type": "node",
"request": "launch",
"preLaunchTask": "rust-tests",
}
]
}在 .vscode 下添加 tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "typescript-test",
"command": "npm",
"args": [
"test",
"--alwaysStric",
"--strictBindCallApply",
"--strictFunctionTypes",
"--target",
"ES2022",
"typescript/test.ts"
],
"type": "shell"
},
{
"label": "typescript-tests",
"command": "npm",
"args": [
"test",
"--alwaysStric",
"--strictBindCallApply",
"--strictFunctionTypes",
"--target",
"ES2022",
"typescript/problems.test.ts"
],
"type": "shell"
},
{
"label": "python-test",
"command": "python",
"args": [
"python/test.py"
],
"type": "shell"
},
{
"label": "python-tests",
"command": "python",
"args": [
"python/tests.py"
],
"type": "shell"
},
{
"label": "golang-test",
"command": "go",
"args": [
"test",
"-tags=goexperiment.jsonv2",
"golang/solution_test.go",
"golang/test_basic.go",
"-test.timeout",
"3s",
"-v"
],
"type": "shell"
},
{
"label": "golang-tests",
"command": "go",
"args": [
"test",
"-tags=goexperiment.jsonv2",
"golang/problems_test.go",
"golang/test_basic.go",
"-test.timeout",
"10s",
"-v"
],
"type": "shell"
},
{
"label": "cpp-test",
"type": "shell",
"command": "sh",
"args": [
"-c",
"bazel fetch --force daily && bazel test --cxxopt=-std=c++23 --cxxopt=-O2 --cxxopt=-fsanitize=address --linkopt=-fsanitize=address --test_timeout=3 --test_output=all //:daily_test"
]
},
{
"label": "cpp-tests",
"type": "shell",
"command": "sh",
"args": [
"-c",
"bazel fetch --force daily && bazel test --cxxopt=-std=c++23 --cxxopt=-O2 --cxxopt=-fsanitize=address --linkopt=-fsanitize=address --test_timeout=10 --test_output=all $(bazel query \"filter(\\\"plan_*\\\", kind(cc_test, //...))\")"
]
},
{
"label": "java-test",
"command": "mvn",
"args": [
"test",
"-Dtest=\"qubhjava.test.TestMain\""
],
"type": "shell"
},
{
"label": "java-tests",
"command": "mvn",
"args": [
"test",
"-Dtest=\"qubhjava.test.ProblemsTest\""
],
"type": "shell"
},
{
"label": "rust-test",
"command": "cargo",
"args": [
"test",
"--test",
"solution_test"
],
"type": "shell"
},
{
"label": "rust-tests",
"command": "cargo",
"args": [
"test",
"--test",
"solutions_test"
],
"type": "shell"
}
]
}配置 GitHub Action Secrets
用于每日自动脚本。{SECRETS: TOKEN}

注意: 为 actions 添加 PROBLEM_FOLDER 才能正常工作。
注意: 除非你知道自己在做什么,否则不要启用 Semantic Release。
- Benhao Demo (Python3)
- SilentSliver Demo (Java)
- LazyKindMan Demo (Golang)



