leetcode-cli的功能在不斷完善抢韭,同時也帶來了一些實(shí)現(xiàn)上的問題:
- 新功能的不斷加入提高了代碼邏輯的復(fù)雜度松嘶,容易出bug。
- 不同功能的邏輯混在一起俐芯,難以理解和維護(hù)棵介。
- 部分功能不是所有人都需要,也不是對所有編程語言都適用吧史。
- 所有新功能的發(fā)布必須更新現(xiàn)有l(wèi)eetcode-cli的版本。
就此在2.0.0版本中我們重構(gòu)了現(xiàn)有代碼唠雕,引入了插件機(jī)制來解決上面遇到的問題贸营,其優(yōu)勢在于:
- 可定制:用戶可按照自己的喜好選擇性安裝某些插件。
- 可擴(kuò)展:新插件可以很容易的集成進(jìn)來岩睁,即插即用钞脂,現(xiàn)有l(wèi)eetcode-cli版本幾乎無需改動。
- 可維護(hù):每個插件實(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
在線提交測試之前扇调,先使用cpplint對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++代碼抢肛,便于本地一些簡單的代碼調(diào)試狼钮。不過僅限于部分題目,有些題目(比如系統(tǒng)設(shè)計類)需要構(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;
}