leetcode-cli: 体验不一样的刷题风 (4) 插件篇

leetcode-cli的功能在不断完善,同时也带来了一些实现上的问题:

  • 新功能的不断加入提高了代码逻辑的复杂度,容易出bug。
  • 不同功能的逻辑混在一起,难以理解和维护。
  • 部分功能不是所有人都需要,也不是对所有编程语言都适用。
  • 所有新功能的发布必须更新现有leetcode-cli的版本。

就此在2.0.0版本中我们重构了现有代码,引入了插件机制来解决上面遇到的问题,其优势在于:

  • 可定制:用户可按照自己的喜好选择性安装某些插件。
  • 可扩展:新插件可以很容易的集成进来,即插即用,现有leetcode-cli版本几乎无需改动。
  • 可维护:每个插件实现为独立的js文件,逻辑清晰便于管理。

https://github.com/skygragon/leetcode-cli-plugins 提供了一些可用的第三方插件,并在不断完善中。目前提供的插件有:

company.js

可按照公司来筛选题目列表,这些信息主要收集于网上相似的题目,比如lintcode, careercup等。

$ leetcode list -q hL -t facebook
      [410] Split Array Largest Sum                                      Hard   (36.60 %)
    ✔ [301] Remove Invalid Parentheses                                   Hard   (35.03 %)
    ✔ [297] Serialize and Deserialize Binary Tree                        Hard   (33.12 %)
      [282] Expression Add Operators                                     Hard   (29.55 %)
      [273] Integer to English Words                                     Hard   (21.98 %)
      [218] The Skyline Problem                                          Hard   (27.00 %)
    ✔ [146] LRU Cache                                                    Hard   (17.53 %)
    ✔ [128] Longest Consecutive Sequence                                 Hard   (36.63 %)
    ✔ [ 85] Maximal Rectangle                                            Hard   (27.66 %)
    ✔ [ 76] Minimum Window Substring                                     Hard   (25.14 %)
    ✔ [ 68] Text Justification                                           Hard   (18.95 %)
    ✔ [ 57] Insert Interval                                              Hard   (27.46 %)
    ✔ [ 44] Wildcard Matching                                            Hard   (19.93 %)
    ✔ [ 25] Reverse Nodes in k-Group                                     Hard   (30.61 %)
    ✔ [ 23] Merge k Sorted Lists                                         Hard   (27.08 %)
    ✔ [ 10] Regular Expression Matching                                  Hard   (24.06 %)

cpp.lint.js

在线提交测试之前,先使用cpplint对c++代码进行静态分析。

$ leetcode test 1.two-sum.cpp

Input data:
[3,2,4]
6

Running cpplint ...

[ERROR] 1.two-sum.cpp:29:  public: should be indented +1 space inside class Solution  [whitespace/indent] [3]
[ERROR] 1.two-sum.cpp:30:  Is this a non-const reference? If so, make const or use a pointer: vector<int>& nums  [runtime/references] [2]
[ERROR] 1.two-sum.cpp:31:  Line ends in whitespace.  Consider deleting these extra spaces.  [whitespace/end_of_line] [4]
[ERROR] 1.two-sum.cpp:31:  Redundant blank line at the start of a code block should be deleted.  [whitespace/blank_line] [2]
[ERROR] 1.two-sum.cpp:31:  Redundant blank line at the end of a code block should be deleted.  [whitespace/blank_line] [3]

cpp.run.js

本地直接运行测试c++代码,便于本地一些简单的代码调试。不过仅限于部分题目,有些题目(比如系统设计类)需要构建额外的桩代码才能运行,有些得不偿失。

$ leetcode test 001.two-sum.cpp --local

Input data:
[3,2,4]
6

Testing locally ...

[1,2]

solution.discuss

显示在leetcode.com讨论区里点赞最多的解法。

$ leetcode show 1 --solution

Accepted C++ O(n) Solution

https://discuss.leetcode.com/topic/3294/accepted-c-o-n-solution

* Lang:  cpp
* Votes: 221

vector<int> twoSum(vector<int> &numbers, int target)
{
    //Key is the number and value is its index in the vector.
    unordered_map<int, int> hash;
    vector<int> result;
    for (int i = 0; i < numbers.size(); i++) {
        int numberToFind = target - numbers[i];

            //if numberToFind is found in map, return them
        if (hash.find(numberToFind) != hash.end()) {
                    //+1 because indices are NOT zero based
            result.push_back(hash[numberToFind] + 1);
            result.push_back(i + 1);
            return result;
        }

            //number was not found. Put it in the map.
        hash[numbers[i]] = i;
    }
    return result;
}

传送门

    原文作者:skygragon
    原文地址: https://www.jianshu.com/p/557bb3100c32
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞