npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

getmdfromleetcode

v1.1.1

Published

A command line tool to fetch LeetCode problem content and convert it to Markdown

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