Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

LeetCode Sync - 题单同步工具

一、简介

LeetCode Sync 是一个强大的命令行工具(兼 MCP Server),旨在帮助你将 LeetCode/力扣 上的指定题单以及你的提交记录和代码同步到本地。

核心功能:

  1. 自动同步提交:根据题单自动拉取你曾经 AC 的最新代码,并按分类写入本地 leetcode-practice/ 目录。
  2. 题目描述补全:自动抓取题目中文描述,生成美观的 Markdown 文件。
  3. 本地代码提交:在本地修改完代码后,可通过命令行一键提交到 LeetCode 并获取判题结果。
  4. MCP Server 支持:内置 MCP Server,可被 Cursor / Trae 等 AI 编程助手直接调用,实现「AI 自动拉取题目 -> AI 本地解答 -> AI 自动提交」的工作流。

二、项目结构

  • config.json:包含站点配置和 Cookie 等鉴权信息。
  • problems.json:题单数据(包含题目的 slug、分类、难度等映射)。
  • 核心模块包括:client(GraphQL 请求)、syncer(同步与文件处理逻辑)。

三、快速开始

1. 编译构建

确保你已安装 Go (>=1.20)。

git clone https://github.com/Orlandoo24/leetcode-sync.git
cd leetcode-sync
go build -o leetcode-sync .

2. 配置环境

cp config.json.example config.json

编辑 config.json,填入从浏览器获取的 Cookie:

  • 登录 https://leetcode.cn
  • 按 F12 打开开发者工具 → Application → Cookies
  • 复制 LEETCODE_SESSION、csrftoken 以及 cf_clearance 的值填入配置中。
{
  "site": "leetcode.cn",
  "cookies": {
    "LEETCODE_SESSION": "你的会话值",
    "csrftoken": "你的 csrf 值",
    "cf_clearance": "你的 cf_clearance 值"
  },
  "output_dir": "./leetcode-practice",
  "delay_ms": 500
}

3. 使用方法

验证配置

./leetcode-sync ping

执行同步

将 problems.json 中的题目及你的提交记录拉取到本地:

./leetcode-sync sync
# 或指定每题最多拉取几份提交、语言过滤:
./leetcode-sync sync --limit 3 --lang go

执行后,你的本地将生成如下目录结构:

  • ./leetcode-practice/{分类}/{题号-题名}/solution.go:主解法
  • ./leetcode-practice/{分类}/{题号-题名}/problem.md:题目描述

提交解法

在本地编写完代码后,可直接提交:

./leetcode-sync submit --slug two-sum --file ./leetcode-practice/HashTable/1-两数之和/solution.go --lang golang

其他辅助命令

  • fix:修复已拉取代码的编译问题(自动注入 ListNode/TreeNode 等类型定义)
  • fix-header:为已拉取的解法文件第一行补充力扣题目链接
  • fetch-desc:批量拉取/补全题目描述

四、MCP 集成

本工具支持作为 MCP Server 运行,为 AI 助手(如 Cursor, Trae)提供操作力扣的能力。

可以在编辑器的 MCP 配置(如 .mcp.json 或 Cursor 的 MCP 配置界面)中添加本工具:

{
  "mcpServers": {
    "leetcode_sync": {
      "command": "/绝对路径/到/leetcode-sync",
      "args": ["mcp"],
      "env": {
        "CONFIG_PATH": "/绝对路径/到/config.json",
        "PROBLEMS_PATH": "/绝对路径/到/problems.json"
      }
    }
  }
}

配置完成后,可以直接对 AI 说「帮我同步 LeetCode 题单」触发工具调用。

五、注意事项

安全警告:config.json 包含敏感的个人身份凭证(Cookie),请绝对不要将其提交到 Git 仓库。已在 .gitignore 中默认忽略。

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages