#leetcode #config-file #game #command-line-interface #shell-completion #cli

bin+lib leetcode-cli

Rust 编写的 LeetCode 命令行界面

显示软件包…

55 个版本

新版本 0.4.5 2024 年 8 月 24 日
0.4.4 2024 年 7 月 5 日
0.4.3 2023 年 12 月 19 日
0.4.2 2023 年 7 月 11 日
0.1.2 2019 年 12 月 31 日

#29 in #games

Download history 1/week @ 2024-05-05 3/week @ 2024-05-19 12/week @ 2024-05-26 14/week @ 2024-06-02 7/week @ 2024-06-09 22/week @ 2024-06-16 10/week @ 2024-06-23 115/week @ 2024-06-30 27/week @ 2024-07-07 19/week @ 2024-07-14 10/week @ 2024-07-21 7/week @ 2024-07-28 16/week @ 2024-08-04 7/week @ 2024-08-11 103/week @ 2024-08-18

每月 134 次下载

MIT 许可证

130KB
3K SLoC

leetcode-cli

Rust crate doc downloads telegram LICENSE

安装

# Required dependencies:
#
#  gcc
#  libssl-dev
#  libdbus-1-dev
#  libsqlite3-dev

cargo install leetcode-cli
Shell 完整性

对于 Bash 和 Zsh(默认从环境变量 $SHELL 中获取 .bash_profile.zshrc

eval "$(leetcode completions)"

将上面的行复制到 .bash_profile.zshrc

您也可以使用它来获取特定的 Shell 配置。

leetcode completions fish

如果没有提供参数,Shell 将从 SHELL 环境变量中推断。

用法

请确保您已使用 Firefox 登录到 leetcode.com。有关为什么需要这样做,请参阅 Cookies

leetcode 0.4.0
May the Code be with You 👻

USAGE:
    leetcode [FLAGS] [SUBCOMMAND]

FLAGS:
    -d, --debug      debug mode
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    data    Manage Cache [aliases: d]
    edit    Edit question by id [aliases: e]
    exec    Submit solution [aliases: x]
    list    List problems [aliases: l]
    pick    Pick a problem [aliases: p]
    stat    Show simple chart about submissions [aliases: s]
    test    Edit question by id [aliases: t]
    help    Prints this message or the help of the given subcommand(s)

示例

要配置 leetcode-cli,在 ~/.leetcode/leetcode.toml 创建一个文件)

[code]
editor = 'emacs'
# Optional parameter
editor_args = ['-nw']
# Optional environment variables (ex. [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ])
editor_envs = []
lang = 'rust'
edit_code_marker = false
start_marker = ""
end_marker = ""
# if include problem description
comment_problem_desc = false
# comment syntax
comment_leading = ""
test = true

[cookies]
csrf = '<your-leetcode-csrf-token>'
session = '<your-leetcode-session-key>'
# leetcode.com or leetcode.cn
site = "leetcode.com"

[storage]
cache = 'Problems'
code = 'code'
root = '~/.leetcode'
scripts = 'scripts'
配置说明
[code]
editor = 'emacs'
# Optional parameter
editor_args = ['-nw']
# Optional environment variables (ex. [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ])
editor_envs = []
lang = 'rust'
edit_code_marker = true
start_marker = "start_marker"
end_marker = "end_marker"
# if include problem description
comment_problem_desc = true
# comment syntax
comment_leading = "//"
test = true

[cookies]
csrf = '<your-leetcode-csrf-token>'
session = '<your-leetcode-session-key>'

[storage]
cache = 'Problems'
code = 'code'
root = '~/.leetcode'
scripts = 'scripts'

如果我们像之前那样更改配置,使用 leetcode edit 15 后,我们将得到以下代码。

// Category: algorithms
// Level: Medium
// Percent: 32.90331%

// Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
//
// Notice that the solution set must not contain duplicate triplets.
//
//  
// Example 1:
//
// Input: nums = [-1,0,1,2,-1,-4]
// Output: [[-1,-1,2],[-1,0,1]]
// Explanation:
// nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
// nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
// nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
// The distinct triplets are [-1,0,1] and [-1,-1,2].
// Notice that the order of the output and the order of the triplets does not matter.
//
//
// Example 2:
//
// Input: nums = [0,1,1]
// Output: []
// Explanation: The only possible triplet does not sum up to 0.
//
//
// Example 3:
//
// Input: nums = [0,0,0]
// Output: [[0,0,0]]
// Explanation: The only possible triplet sums up to 0.
//
//
//  
// Constraints:
//
//
// 3 <= nums.length <= 3000
// -10⁵ <= nums[i] <= 10⁵
//

// start_marker
impl Solution {
pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {

    }

}
// end_marker


某些代码检查工具/lsps 除非导入必要的库,否则会抛出错误。如果设置了 inject_before 键,leetcode-cli 可以自动生成此样板代码。同样,如果您想在本地上测试代码,可以使用 inject_after 自动化。对于 C++,这可能看起来像

[code]
inject_before = ["#include<bits/stdc++.h>", "using namespace std;"]
inject_after = ["int main() {\n    Solution solution;\n\n}"]

1. 选择

leetcode pick 1
leetcode pick --name "Two Sum"
[1] Two Sum is on the run...


Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

--------------------------------------------------

Example:


Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

2. 编辑

leetcode edit 1
# struct Solution;
impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
        use std::collections::HashMap;
        let mut m: HashMap<i32, i32> = HashMap::new();

        for (i, e) in nums.iter().enumerate() {
            if let Some(v) = m.get(&(target - e)) {
                return vec![*v, i as i32];
            }

            m.insert(*e, i as i32).unwrap_or_default();
        }

        return vec![];
    }
}

3. 测试

leetcode test 1

  Accepted       Runtime: 0 ms

  Your input:    [2,7,11,15], 9
  Output:        [0,1]
  Expected:      [0,1]

4. 执行

leetcode exec 1

  Success

  Runtime: 0 ms, faster than 100% of Rustonline submissions for Two Sum.

  Memory Usage: 2.4 MB, less than 100% of Rustonline submissions for Two Sum.


Cookie

leetcode-cli 的 Cookie 插件可以在 macOS 和 Linux 上运行。 如果您在另一平台上,缓存 Cookie 可能存在问题,您可以手动将 LeetCode Cookie 输入到配置文件中。

[cookies]
csrf = "..."
session = "..."

例如,使用 Firefox(登录 LeetCode 后)

步骤 1

打开 Firefox,按 F12,然后点击 Storage 选项卡。

步骤 2

展开左侧的 Cookies 选项卡,并选择 https://leetcode.com

步骤 2

ValueLEETCODE_SESSIONcsrftoken 复制到配置文件中的 sessioncsrf,分别对应

[cookies]
csrf = '<your-leetcode-csrf-token>'
session = '<your-leetcode-session-key>'

可编程

如果您想使用自定义Python脚本来过滤LeetCode题目,请将以下内容添加到配置文件中

[storage]
scripts = "scripts"

然后编写脚本

# ~/.leetcode/scripts/plan1.py
import json;

def plan(sps, stags):
    ##
    # `print` in python is supported,
    # if you want to know the data structures of these two args,
    # just print them
    ##
    problems = json.loads(sps)
    tags = json.loads(stags)

    ret = []
    tm = {}
    for tag in tags:
        tm[tag["tag"]] = tag["refs"];

    for i in problems:
        if i["level"] == 1 and str(i["id"]) in tm["linked-list"]:
            ret.append(str(i["id"]))

    # return is `List[string]`
    return ret

然后运行 list 并使用您刚才编写的过滤器

leetcode list -p plan1

就是这样!享受吧!

贡献

请在 Cargo.tomlauthors 字段中添加您的姓名和电子邮件,无需拘泥

许可证

MIT

依赖项

~36–54MB
~882K SLoC