Skip to main content

leetcode-solutions

一个自定义的LeetCode题解PyPI包,包含最优Python解法,支持按题号、难度、题型、周赛等多维度导入使用。

特性

  • ✅ 多维度分类:支持按题号、难度(简单/中等/困难/会员/面试)、题型、周赛分类
  • ✅ 最优解法:每道题只保留最高效的算法实现
  • ✅ 灵活导入:支持多种导入方式,使用便捷
  • ✅ 元数据查询:提供题目信息查询功能
  • ✅ 代码复用:核心实现只写一次,各分类通过导入复用

安装

pip install leetcode-solutions

使用示例

1. 按题号导入(最常用)

from leetcode_solutions.by_id import q0001

# 使用函数式接口
result = q0001.two_sum([2, 7, 11, 15], 9)
print(result)  # [0, 1]

# 使用类式接口(兼容LeetCode提交格式)
solution = q0001.Solution()
result = solution.twoSum([3, 2, 4], 6)
print(result)  # [1, 2]

2. 按难度导入

from leetcode_solutions.by_difficulty.easy import q0001
from leetcode_solutions.by_difficulty.medium import q0002
from leetcode_solutions.by_difficulty.hard import q0003
from leetcode_solutions.by_difficulty.premium import q0004
from leetcode_solutions.by_difficulty.interview import q0001

# 使用方式相同
result = q0001.two_sum([2, 7, 11, 15], 9)

3. 按题型导入

from leetcode_solutions.by_topic.array import q0001
from leetcode_solutions.by_topic.linked_list import q0002
from leetcode_solutions.by_topic.dp import q0005

# 使用方式相同
result = q0001.two_sum([2, 7, 11, 15], 9)

4. 按周赛导入

from leetcode_solutions.by_contest.weekly_300 import q0001
from leetcode_solutions.by_contest.biweekly_100 import q0002

# 使用方式相同
result = q0001.two_sum([2, 7, 11, 15], 9)

5. 查询题目信息

from leetcode_solutions import (
    get_problems_by_difficulty,
    get_problems_by_topic,
    get_problems_by_contest,
    get_problem_info,
    PROBLEM_METADATA
)

# 查询所有简单题
easy_problems = get_problems_by_difficulty("easy")
print(easy_problems)  # [1, ...]

# 查询所有数组题
array_problems = get_problems_by_topic("array")
print(array_problems)  # [1, ...]

# 查询所有周赛题
contest_problems = get_problems_by_contest("weekly_300")
print(contest_problems)  # [1, ...]

# 查询题目详细信息
info = get_problem_info(1)
print(info)
# {
#     "id": 1,
#     "title": "两数之和",
#     "slug": "two-sum",
#     "difficulty": "easy",
#     "topics": ["array", "hash_table"],
#     "is_premium": False,
#     "is_interview": True,
#     "contests": ["weekly_300"]
# }

6. 使用工具类

from leetcode_solutions.utils import ListNode, TreeNode

# 创建链表
head = ListNode.from_list([1, 2, 3, 4])
print(head.to_list())  # [1, 2, 3, 4]

# 创建树
root = TreeNode.from_list([1, 2, 3, None, 5])

项目结构

leetcode_solutions/
├── __init__.py          # 核心导出文件
├── by_id/               # 按题号分类(核心实现层)
│   ├── __init__.py
│   ├── q0001.py
│   ├── q0002.py
│   └── ...
├── by_difficulty/       # 按难度分类(复用by_id)
│   ├── easy/
│   ├── medium/
│   ├── hard/
│   ├── premium/
│   └── interview/
├── by_topic/            # 按题型分类(复用by_id)
│   ├── array/
│   ├── linked_list/
│   ├── dp/
│   └── ...
├── by_contest/          # 按周赛分类(复用by_id)
│   ├── weekly_300/
│   ├── biweekly_100/
│   └── ...
├── utils/               # 通用工具类
│   ├── linked_list.py
│   ├── tree.py
│   └── common.py
└── metadata.py          # 元数据映射

开发指南

添加新题目

  1. 在 leetcode_solutions/by_id/ 下创建 qXXXX.py(4位题号,不足补0)
  2. 实现最优解法(函数式 + 类式)
  3. 在 metadata.py 中添加题目元数据
  4. 在对应的 by_difficulty/、by_topic/、by_contest/ 目录下创建导入文件

更新版本

修改 setup.py 中的 __version__,然后重新打包发布。

许可证

MIT License

贡献

欢迎提交Issue和Pull Request!

Metadata

Release files for leetcode-solutions 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for leetcode-solutions 0.1.0
File Size Uploaded
leetcode_solutions-0.1.0.tar.gz 17.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for leetcode-solutions 0.1.0
File Interpreter ABI Platform
leetcode_solutions-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 47.0 kB

Release files / leetcode_solutions-0.1.0.tar.gz

Download URL leetcode_solutions-0.1.0.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e5e1b293f0bdfc7a6a24a237ad65f7c2a9c0a8a5935dc54a6689aa60a14d6f33
BLAKE2b-256 checksum
How to use checksums
5a885074b8258328a482af9fde9f4796781cf7c0812513f4d179d21b78dcb300
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release files / leetcode_solutions-0.1.0-py3-none-any.whl

Download URL leetcode_solutions-0.1.0-py3-none-any.whl
Size 29.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9940f8a5dd7cf1cf7d8e0fd3943837a7e91f0e33381c4fda96cecad3c268cdc3
BLAKE2b-256 checksum
How to use checksums
86e9f909bde2e6fa3499ab78c23fe294a37dea0aff785d259508ec9c5c02d0f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page