A command-line tool written in Rust for LeetCode practice. Randomly select problems, download them locally, write code, and check answers.
| Metric | Status |
|---|---|
| Line Coverage | 77% (170 tests) |
| Tests Passing | ✅ 170/170 |
| Clippy | ✅ Clean |
| Format | ✅ Clean |
- Quick Start Guide - Get started in 5 minutes
- Usage Examples - Detailed usage examples and workflows
- Contributing Guide - Development setup and contribution guidelines
- Project Summary - Project overview and architecture
- 🎲 Random Problem Selection - Select problems randomly by difficulty and tags
- 📥 Local Download - Download problem descriptions and code templates
- 🧪 Local Testing - Run Rust unit tests locally
- 📤 Submit Solutions - Submit directly to LeetCode and view results
- 📋 Problem List - View all problems and their status
- 🔍 Problem Details - View detailed problem descriptions
# Clone the repository
git clone <repository-url>
cd leetcode-cli
# Build release version
cargo build --release
# Add binary to PATH
cp target/release/leetcode-cli ~/.local/bin/- Rust (nightly toolchain as specified in
rust-toolchain.toml) - Cargo
To submit solutions, you need to login to LeetCode to get session cookie and CSRF token:
# Method 1: Interactive login
leetcode-cli login
# Method 2: Provide credentials directly
leetcode-cli login --session "your_session_cookie" --csrf "your_csrf_token"How to get cookies from your browser:
-
Login to LeetCode in your browser at https://leetcode.com
-
Open Developer Tools:
- Chrome/Edge: Press
F12orCtrl+Shift+I(Windows/Linux),Cmd+Option+I(Mac) - Firefox: Press
F12orCtrl+Shift+I(Windows/Linux),Cmd+Option+I(Mac)
- Chrome/Edge: Press
-
Navigate to Application/Storage tab:
- Chrome/Edge: Click "Application" tab → "Cookies" → "https://leetcode.com"
- Firefox: Click "Storage" tab → "Cookies" → "https://leetcode.com"
-
Copy the cookie values:
- Find
LEETCODE_SESSION- copy its value (long JWT string) - Find
csrftoken- copy its value (shorter alphanumeric string)
- Find
-
Paste into the CLI when prompted or use the
--sessionand--csrfflags
Install a cookie manager extension for easier access:
- Chrome: "EditThisCookie" or "Cookie-Editor"
- Firefox: "Cookie Quick Manager"
Then:
- Click the extension icon while on leetcode.com
- Find and copy
LEETCODE_SESSIONandcsrftoken
- Open Developer Tools (F12)
- Go to "Console" tab
- Run this JavaScript:
console.log('LEETCODE_SESSION:', document.cookie.match(/LEETCODE_SESSION=([^;]+)/)?.[1]); console.log('csrftoken:', document.cookie.match(/csrftoken=([^;]+)/)?.[1]);
Important Security Notes:
- These credentials are stored in
~/.config/leetcode-cli/config.toml(Linux/Mac) or%APPDATA%/leetcode-cli/config.toml(Windows) - Session expires after some time (days/weeks) - re-login when submission fails
- Never share or commit these credentials - treat them like passwords
- The
LEETCODE_SESSIONcookie grants access to your LeetCode account
# Completely random
leetcode-cli pick
# Filter by difficulty
leetcode-cli pick --difficulty easy
leetcode-cli pick --difficulty medium
leetcode-cli pick --difficulty hard
# Specify problem ID
leetcode-cli pick --id 1# Run tests for a problem
leetcode-cli test --id 1
# Or use cargo directly
cargo test p0001_two_sum# Submit current problem solution
leetcode-cli submit --id 1
# Specify solution file path
leetcode-cli submit --id 1 --file ./my_solution.rs# View all problems
leetcode-cli list
# Filter by difficulty
leetcode-cli list --difficulty medium
# Filter by status
leetcode-cli list --status solved # Solved
leetcode-cli list --status attempting # Attempting
leetcode-cli list --status unsolved # Unsolvedleetcode-cli show --id 1When downloading a problem, a Rust code template is automatically generated with the problem description embedded in doc comments and pre-populated test cases:
#![allow(dead_code)]
/// Problem: Two Sum
/// Difficulty: Easy
/// URL: https://leetcode.com/problems/two-sum/
///
/// [Problem description in doc comments...]
pub struct Solution;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// TODO: Implement your solution here
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_case_0001_1() {
// Input: nums = [2,7,11,15], target = 9
// Expected: [0,1]
// TODO: Add test implementation
}
#[test]
fn test_case_0001_2() {
// Input: nums = [3,2,4], target = 6
// Expected: [1,2]
// TODO: Add test implementation
}
// Additional test cases for edge cases...
}# 1. Login
leetcode-cli login
# 2. Randomly select a medium difficulty problem
leetcode-cli pick --difficulty medium
# 3. Enter problem directory and write solution
vim src/solutions/p0002_add_two_numbers.rs
# 4. Local testing
leetcode-cli test --id 2
# 5. Submit solution
leetcode-cli submit --id 2Configuration file location:
- Linux/macOS:
~/.config/leetcode-cli/config.toml - Windows:
%APPDATA%/leetcode-cli/config.toml
Example configuration:
session_cookie = "your_session_cookie"
csrf_token = "your_csrf_token"
default_language = "rust"
workspace_path = "/path/to/your/workspace"
editor = "vim"| Command | Description |
|---|---|
leetcode-cli login |
Login to LeetCode |
leetcode-cli pick |
Random problem selection |
leetcode-cli pick -d medium |
Random medium difficulty problem |
leetcode-cli test -i 1 |
Local testing |
leetcode-cli submit -i 1 |
Submit solution |
leetcode-cli list |
View problem list |
leetcode-cli show -i 1 |
View problem details |
- Session Cookie Security: Login credentials are saved in local config file, please keep it safe
- Submission Rate Limit: LeetCode has submission rate limits, too frequent submissions may be temporarily restricted
- Network Connection: Make sure environment variables are set correctly when using proxy
- Check network connection
- Confirm access to https://leetcode.com
- Confirm correct login
- Check if session cookie has expired (need to re-login)
- Confirm correct code format
- Ensure Rust and Cargo are installed
- Check if solution.rs file has syntax errors
Apache License 2.0
Issues and Pull Requests are welcome!