LeetCode题解包,支持按题号/难度/题型/周赛导入最优Python解法
Project description
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 # 元数据映射
开发指南
添加新题目
- 在
leetcode_solutions/by_id/下创建qXXXX.py(4位题号,不足补0) - 实现最优解法(函数式 + 类式)
- 在
metadata.py中添加题目元数据 - 在对应的
by_difficulty/、by_topic/、by_contest/目录下创建导入文件
更新版本
修改 setup.py 中的 __version__,然后重新打包发布。
许可证
MIT License
贡献
欢迎提交Issue和Pull Request!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
leetcode_solutions-0.1.0.tar.gz
(17.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file leetcode_solutions-0.1.0.tar.gz.
File metadata
- Download URL: leetcode_solutions-0.1.0.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5e1b293f0bdfc7a6a24a237ad65f7c2a9c0a8a5935dc54a6689aa60a14d6f33
|
|
| MD5 |
1b1b95d7b68598643a329393260f19a2
|
|
| BLAKE2b-256 |
5a885074b8258328a482af9fde9f4796781cf7c0812513f4d179d21b78dcb300
|
File details
Details for the file leetcode_solutions-0.1.0-py3-none-any.whl.
File metadata
- Download URL: leetcode_solutions-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9940f8a5dd7cf1cf7d8e0fd3943837a7e91f0e33381c4fda96cecad3c268cdc3
|
|
| MD5 |
5f4d61d12767154dc516f3cdf2ac1357
|
|
| BLAKE2b-256 |
86e9f909bde2e6fa3499ab78c23fe294a37dea0aff785d259508ec9c5c02d0f5
|