leetcode-cli: 體驗(yàn)不一樣的刷題風(fēng) (4) 插件篇

leetcode-cli的功能在不斷完善,同時(shí)也帶來了一些實(shí)現(xiàn)上的問題:

  • 新功能的不斷加入提高了代碼邏輯的復(fù)雜度,容易出bug。
  • 不同功能的邏輯混在一起,難以理解和維護(hù)。
  • 部分功能不是所有人都需要,也不是對(duì)所有編程語言都適用。
  • 所有新功能的發(fā)布必須更新現(xiàn)有l(wèi)eetcode-cli的版本。

就此在2.0.0版本中我們重構(gòu)了現(xiàn)有代碼,引入了插件機(jī)制來解決上面遇到的問題,其優(yōu)勢(shì)在于:

  • 可定制:用戶可按照自己的喜好選擇性安裝某些插件。
  • 可擴(kuò)展:新插件可以很容易的集成進(jìn)來,即插即用,現(xiàn)有l(wèi)eetcode-cli版本幾乎無需改動(dòng)。
  • 可維護(hù):每個(gè)插件實(shí)現(xiàn)為獨(dú)立的js文件,邏輯清晰便于管理。

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

company.js

可按照公司來篩選題目列表,這些信息主要收集于網(wǎng)上相似的題目,比如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

在線提交測(cè)試之前,先使用cpplint對(duì)c++代碼進(jìn)行靜態(tài)分析。

$ 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

本地直接運(yùn)行測(cè)試c++代碼,便于本地一些簡單的代碼調(diào)試。不過僅限于部分題目,有些題目(比如系統(tǒng)設(shè)計(jì)類)需要構(gòu)建額外的樁代碼才能運(yùn)行,有些得不償失。

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

Input data:
[3,2,4]
6

Testing locally ...

[1,2]

solution.discuss

顯示在leetcode.com討論區(qū)里點(diǎn)贊最多的解法。

$ 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;
}

傳送門

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容