getmdfromleetcode
v1.1.1
Published
A command line tool to fetch LeetCode problem content and convert it to Markdown
Maintainers
Readme
Get Markdown from LeetCode
一个命令行工具,用于从 LeetCode 题目页面提取内容并转换为 Markdown 格式。
功能
- 从 LeetCode 题目页面抓取题目内容
- 将题目内容转换为标准 Markdown 格式
- 支持 LeetCode 中国站 (leetcode.cn)
- 提取题目标题、难度等级和详细描述
- 正确格式化示例代码和输入输出
- 提取官方题解内容(包括解题思路和代码实现)
- 支持多种编程语言的代码展示
- 数学公式自动转换为 LaTeX 格式
- 支持将生成内容复制到系统剪贴板
安装
确保你的系统已安装 Node.js 和 yarn。
克隆或下载此仓库后,安装依赖:
yarn install或者全局安装:
yarn global add getmdfromleetcode或者使用 npm:
npm install -g getmdfromleetcode使用方法
作为命令行工具使用
node index.js -u <leetcode-problem-url>例如:
node index.js -u https://leetcode.cn/problems/two-sum/安装后也可以直接使用命令:
getmdfromleetcode -u <leetcode-problem-url>参数说明
-u, --url: 指定 LeetCode 题目 URL(必填)-e, --english: 切换到英文内容显示-r, --raw: 输出原始 HTML 格式内容-c, --clipboard: 将输出内容复制到系统剪贴板
示例:
# 获取中文题目内容
getmdfromleetcode -u https://leetcode.cn/problems/two-sum/
# 获取英文题目内容
getmdfromleetcode -u https://leetcode.cn/problems/two-sum/ -e
# 获取原始 HTML 格式内容
getmdfromleetcode -u https://leetcode.cn/problems/two-sum/ -r
# 获取题目内容并复制到剪贴板
getmdfromleetcode -u https://leetcode.cn/problems/two-sum/ -c
# 组合使用多个参数
getmdfromleetcode -u https://leetcode.cn/problems/two-sum/ -e -c输出内容
工具会将 LeetCode 题目转换为如下 Markdown 格式:
# 两数之和
**Difficulty:** 简单
**Tags:** 数组, 哈希表
## Description
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出和为目标值 target 的那两个整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
你可以按任意顺序返回答案。
## 示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
**解释:** 因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
## 示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
## 示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
## 提示:
- 2 <= nums.length <= 10^4
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- 只会存在一个有效答案
**进阶:**
你可以想出一个时间复杂度小于 O(n^2) 的算法吗?
## 题解
#### 方法一:暴力枚举
**思路及算法**
最容易想到的方法是枚举数组中的每一个数 `x`,寻找数组中是否存在 `target - x`。
当我们使用遍历整个数组的方式寻找 `target - x` 时,需要注意到每一个位于 `x` 之前的元素都已经和 `x` 匹配过,因此不需要再进行匹配。而每一个元素不能被使用两次,所以我们只需要在 `x` 后面的元素中寻找 `target - x`。
**代码**
```Java [sol1-Java]
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return new int[0];
}
}class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] + nums[j] == target) {
return {i, j};
}
}
}
return {};
}
};class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return []复杂度分析
- 时间复杂度:$O(N^2)$,其中 $N$ 是数组中的元素数量。最坏情况下数组中任意两个数都要被匹配一次。
- 空间复杂度:$O(1)$。
## 技术实现
- 使用 Node.js 开发
- 使用 Cheerio 解析 HTML 内容
- 使用 Yargs 处理命令行参数
- 通过正则表达式提取 JSON 数据
- 使用原生 fetch API 发送网络请求
- 通过 GraphQL API 获取题目和题解数据
## 许可证
MIT