LeetCode
Solving LeetCode locally and keep tracking of your solutions.
Install / Use
/learn @QuBenhao/LeetCodeREADME
LeetCode
Debugging LeetCode locally, Automatic daily problems generator, submit solutions directly and more!
Algorithms in LeetCode by Benhao
Table of Content
How to start
After clone this repo, add a .env file to tell where to locate your problems and solutions (locally).
For remote GitHub Action, add COOKIE (LeetCode cookie), PUSH_KEY (PushDeer notification), PROBLEM_FOLDER (where to
add problems), USER (LeetCode personal page uri), LOG_LEVEL (Log print).
Notice: If you want more than just python3, add LANGUAGES="python3,golang" (and so on in .env)
Example .env file:
PROBLEM_FOLDER="problems"
PUSH_KEY="***[key from PushDeer]"
COOKIE="***[cookie from LeetCode graphql]"
LANGUAGES="python3,golang,java,cpp,typescript,rust"
USER="himymben"
LOG_LEVEL="info"
PYTHONPATH=.
install python3.14 or higher requirements:
pip install -r python/requirements.txt
LeetCode tools all in one
python python/scripts/leetcode.py
usage demo:
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
--------------------------------------------------
Enter the problem folder path (press enter to use default):
Problem folder selected: problems
--------------------------------------------------
Enter your LeetCode cookie (press enter to use default):
--------------------------------------------------
Do you want to update the .env file with this configuration? [y/n, default: n]:
--------------------------------------------------
Please select the main function [0-4, default: 0]:
0. Exit
1. Get problem
2. Submit
3. Clean empty java
4. Clean error rust
2
--------------------------------------------------
Please select the submit method [0-4, default: 0]:
0. Back
1. Daily submit[All selected languages]
2. Daily submit[Select language]
3. Submit specified problem[All selected languages]
4. Submit specified problem[Select language]
3
--------------------------------------------------
Enter the problem ID (e.g., 1, LCR 043, 面试题 01.01, etc.): 1
Starting submission, please wait...
Submitting in language: python3
Waiting for submit result: 1%| | 1/100 [00:00<01:33, 1.06it/s]
INFO:root:[1.two-sum]提交结果
Accepted 63/63个通过的测试用例
执行用时: 3 ms 击败58.9912%
消耗内存: 18.6 MB 击败43.29040000000005%
代码:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
mp = dict()
for i, num in enumerate(nums):
if (t := target - num) in mp:
return [mp[t], i]
mp[num] = i
INFO:root:提交详情: https://leetcode.cn/problems/two-sum/submissions/625296865/ [需登录查看]
INFO:root:题解查看: https://leetcode.cn/problems/two-sum/solutions/
INFO:root:外网查看: https://leetcode.com/problems/two-sum/solutions/
Submitting in language: golang
Waiting for submit result: 1%| | 1/100 [00:00<01:30, 1.09it/s]
INFO:root:[1.two-sum]提交结果
Accepted 63/63个通过的测试用例
执行用时: 0 ms 击败100.0%
消耗内存: 5.7 MB 击败50.63709999999988%
代码:
func twoSum(nums []int, target int) []int {
m := map[int]int{}
for i, num := range nums {
d := target - num
if idx, ok := m[d]; ok {
return []int{idx, i}
}
m[num] = i
}
return nil
}
INFO:root:提交详情: https://leetcode.cn/problems/two-sum/submissions/625296886/ [需登录查看]
INFO:root:题解查看: https://leetcode.cn/problems/two-sum/solutions/
INFO:root:外网查看: https://leetcode.com/problems/two-sum/solutions/
Submission completed.
--------------------------------------------------
Please select the submit method [0-4, default: 0]:
0. Back
1. Daily submit[All selected languages]
2. Daily submit[Select language]
3. Submit specified problem[All selected languages]
4. Submit specified problem[Select language]
Bye!
DeprecationWarning: The tools below is deprecated, please use the new tools in python/scripts/leetcode.py
To directly submit Solution to LeetCode, try any language below:
python python/scripts/submit.py -h
# usage: submit.py [-h] [-id ID] {go,py,ts,js,c++,java,golang,python3,typescript,javascript,cpp,rt,rust}
python python/scripts/submit.py python3 -id=1
python python/scripts/submit.py -id=2 py
python python/scripts/submit.py py
python python/scripts/submit.py golang -id=2
python python/scripts/submit.py cpp -id=1
python python/scripts/submit.py java -id=2
python python/scripts/submit.py typescript -id=1
python python/scripts/submit.py rust -id=1
To get any problem you want, try:
python python/scripts/get_problem.py -h
# usage: get_problem.py [-h] [-id PROBLEM_ID] [-slug PROBLEM_SLUG] [-cate PROBLEM_CATEGORY] [-f] [-all] [-pm] [-debug DEBUG_FILE] [-change] [-sl]
python python/scripts/get_problem.py -id=1
To generate daily problems, try:
python python/scripts/daily_auto.py
To fetch daily submits from LeetCode (requires .env COOKIE or USER to be ready), try:
python python/scripts/daily_submission.py
Some extra tools:
- To backfield existing problems rating, try:
python python/scripts/tools.py rating
- To get the lucky problem of the day, try:
python python/scripts/tools.py lucky
If you think there are too many logs for those scripts in the console, you can set the LOG_LEVEL in the .env file
to ERROR.
Cookie Auto Updater
Auto-update LeetCode CN Cookie from browser to GitHub Secrets or local .env file.
Usage
# Update GitHub Secrets only
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode
# Update local .env only
python python/scripts/leetcode_cookie_updater.py --env .env
# Update both
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode --env .env
# Enable debug logging
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode --log-level DEBUG
# Specify GitHub Token
python python/scripts/leetcode_cookie_updater.py --repo QuBenhao/LeetCode --github-token ghp_xxx
Parameters
| Parameter | Description |
|-----------|-------------|
| --repo REPO | GitHub repo name (e.g., QuBenhao/LeetCode). Skip if not specified |
| --env PATH | Local .env file path. Skip if not specified |
| --log-level LEVEL | Log level: DEBUG, INFO, WARNING, ERROR (default: INFO) |
| --github-token TOKEN | GitHub Token (or set via GITHUB_TOKEN env var) |
GitHub Token Permissions
To update GitHub Secrets, create a token with:
repo(full repo access)workflow(update GitHub Actions)secret(update GitHub Repo Secret)
Steps:
- Go to https://github.com/settings/tokens
- Click "Generate new token (classic)"
- Check
repoandworkflowandsecretpermissions - Generate and save the token
Cron Job
Set up automatic Cookie updates via crontab:
# Run at 2 AM daily
0 2 * * * GITHUB_TOKEN="ghp_xxx" python /path/to/leetcode_cookie_updater.py --repo QuBenhao/LeetCode >> /tmp/leetcode_cookie.log 2>&1
Supported Browsers
- Chrome
- Edge
- Firefox
- Chromium
Supported Languages
Python3
Check Python3 README
Golang
Check Golang README
Java
Check Java README
Cpp
Check Cpp README
Typescript
Check TypeScript README
Rust
Check Rust README
Demo
Fork the repo of your own

Clone your own forked repo
Notice: create your own branch and set it as repo default, and keep master!
Local
Open the code project and installed languages environments as needed.
Test your environment by running languages test, for instance:
if you are facing errors here, contact the author.
Find a cookie from LeetCode (monthly update)

Create your own .env file (Notice: better to use a problem folder other than exists as the author is using them, there will be a lot of conflicts):
PROBLEM_FOLDER=demo
COOKIE="***[cookie from LeetCode graphql]"
LANGUAGES="golang,java"
Create the folder 'demo' based on your own .env
Run scripts to fetch problems, run tests and submit your solution.
If you get problem like this,
it will add the problem and change the tests of your languages as below:

In VsCode,
add launch.json under .vscode
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: 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",
},
{
