Skip to content

Latest commit

 

History

History
480 lines (403 loc) · 11.4 KB

File metadata and controls

480 lines (403 loc) · 11.4 KB

LeetCode

本地调试 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.txt

LeetCode 工具集:

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

Cookie 自动更新工具

自动从浏览器获取 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 Token 权限

如果要更新 GitHub Secrets,需要创建具有以下权限的 Token:

  • repo (完整仓库访问)
  • workflow (更新 GitHub Actions)
  • secret (更新GitHub仓库的Secret)

创建步骤:

  1. 访问 https://github.com/settings/tokens
  2. 点击 "Generate new token (classic)"
  3. 勾选 repoworkflowsecret 权限
  4. 生成并保存 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

支持的语言

Python3

查看 Python3 README

Golang

查看 Golang README

Java

查看 Java README

Cpp

查看 Cpp README

Typescript

查看 TypeScript README

Rust

查看 Rust README

演示

Fork 你自己的仓库: fork.png

克隆你 fork 的仓库

注意:创建你自己的分支并设为默认分支,保留 master 分支!

本地

打开代码项目并安装所需的语言环境。

运行语言测试来验证环境,例如: mvn_test.png 如果遇到错误,请联系作者。

从 LeetCode 获取 cookie(每月更新): cookie.png

创建你自己的 .env 文件(注意:最好使用与作者不同的题目文件夹,会有很多冲突):

PROBLEM_FOLDER=demo
COOKIE="***[LeetCode graphql 的 cookie]"
LANGUAGES="golang,java"

根据你的 .env 创建 'demo' 文件夹

运行脚本获取题目、运行测试并提交你的解决方案。

如果遇到如下问题, get_problem.png 它会添加题目并修改你使用语言的测试文件: new_problem.png changed_golang.png

在 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

配置 GitHub Action Secrets 用于每日自动脚本。{SECRETS: TOKEN} github_settings.png

添加与 .env 类似的值,例如: cookie_key.png

注意:actions 添加 PROBLEM_FOLDER 才能正常工作。

根据需要启用以下 GitHub Actions:

  1. Daily Problems
  2. Submits Check
  3. Sync

注意: 除非你知道自己在做什么,否则不要启用 Semantic Release

演示项目

  1. Benhao Demo (Python3)
  2. SilentSliver Demo (Java)
  3. LazyKindMan Demo (Golang)