Leetcode 簡略題解 - 共567題

寫在開頭:我作為一個老實人,一向非常反感騙贊、收智商稅兩種行為。前幾天看到不止兩三位用戶說自己辛苦寫了乾貨,結果收藏數是點贊數的三倍有餘,感覺自己的無償付出連認同都得不到,很是失望。明白人都知道這是潛水伸手黨的鍋。但同時,截止寫這段話時,我發現這篇Leetcode簡略(得不能再簡略的)題解得到了一千多贊,三千多收藏。所以,在我不向任何人做任何廣告、收任何費用的前提下,麻煩在收藏的同時,順手點個贊(對只收藏不點贊的行為表示明確鄙視)。如果連點一個贊的力氣都沒有,恐怕Leetcode也就甭刷了。拒絕伸手黨,從你我做起。

// To begin with: I for one as an honest man, am always loath at the thought of luring upvotes or making profit from stupid taxes. A few days ago, I saw more than a few zhihuers uttering their disappointment with their informative answers receiving three times as much bookmarks as upvotes. It takes no more than a halfwit to see that lurks are to blame. However, as of the moment of me writing this, this so very brief (hardly more so) summary of Leetcode solutions has around 1k upvotes and 3k bookmarks. Being willing to share things for free requires nothing but a little bit of encouragement and recognition. As such, I recommend those who choose to "bookmark" to add one extra click on the "upvote" button (my sincere despise to all ungrateful lurks), considering the fact that I aint pushing no ads or charging no fees to nobody. IMO, you dont quite stand a chance against hundreds of Leetcode problems if you lack the strength for a mere "upvote". No bloodsucker, your choice matters.

這是一篇由用戶@朱里整理的Leetcode題解。就是我。

// This is a memo for Leetcode problem set, by user @朱里. Thats me.

其中的題解部分會不定期更新,因為題目實在太多了,一次並不能寫完。

// As the number of problems is massive, Ill keep updating this article every once in a while. You dont expect me getting it done overnight, do you?

目前已寫完567題的題解。

// Current progress is 567/585.

目前LeetCode出新題比較頻繁。鑒於付費題目需要交錢才能刷,我打算攢到一兩百題再掏錢。畢竟掏了一個月的錢,一個星期就刷完了,不怎麼划算。按照目前LeetCode題目的難度和質量來看,每次出新題,基本都能馬上搞定,畢竟刷了幾百題,也很難再有什麼新花樣了。

// Currently LeetCode is updating problems at a fast and steady pace. The premium problems are unlocked only after payment, so I decide to take my order when therere a hundred or two available. Basically I dont deem the price quite worth, as you usually finish them off in a week while the payment is for a month. Free problems are fine, just no surprise for me anymore after doing five hundreds of them, I guess.

有人常常花很多時間。考慮做一件事「有什麼用」?你猜我怎麼想,「沒什麼卵用」。

// People tend to waste a lot of time think about the meaning of everything. Know what I think? Nothing. No, none, null, void.

活著、讀書、上學、工作、找個人結婚、安頓下來、放棄一切理想信仰希望、混日子、變老、等死。有意義嗎?沒有意義嗎?

// Live, go to school, be young and foolish, get a job, get laid, settle down, give up your dreams, beliefs and hopes, grow up into a nobody, turn old and stubborn, wither and die. Whats the point? Whats not?

所以,刷幾百道Leetcode,一定能找到工作嗎?別tm廢話了,刷吧。

// So, will Leetcode land me a job after all? Tell you what, just shut the f**k up and do it.

為什麼人生如此艱難?不為什麼。

// Life is tough, for no reason.

所有題中,有大約10題左右不是獨立完成,參考了網上高手的解法。調試用腦子或者紙筆。基本不使用IDE,不編譯運行。

// Among these problems, around 10 were solved after referencing clever solutions from the Internet, the rest were all solved independently. Code was debugged inside my brain or on paper. IDE or compiler were hardly involved.

總體上,代碼盡量追求可讀性,保證時間空間複雜度的優化,並保持風格一致。

// In general, readability is what I care more about (compared to code length). Time and space complexity are bottom line. Consistent code style is maintained at best efforts.

感覺現在面試題的難度也有種「通貨膨脹」的趨勢,以前的「hard」題目和現在的「medium」差不多,看來工作真是越來越難找了。

// There seem to be inflation in tech interview problems as well (AFAIK prices never go down). Those rated "medium" today are no easier than those rated "hard" in the old days. Harder to make a living, isnt it?

代碼庫地址:zhuli19901106/leetcode-2

// Code repo can be found at zhuli19901106/leetcode-2.

關於很多題為什麼一句話(甚至一個詞)帶過,簡言之就是「典型題」。因為一個經過合格訓練的程序員對於兩百行以內的演算法代碼應該有足夠的代碼閱讀能力,以及快速適應新語言的能力。每題的Github代碼足夠提供參考。對於關鍵思路做最少的提示即可,這也是面試所能容忍的程度。超出此程度,免不了被淘汰。在不涉及軟體工程、設計模式、模塊介面設計的情況下,逐字逐句解釋並且配上插圖毫無必要。同時,能五到十分鐘解決的題,是不值得花二十分鐘寫題解的。刷一題發一篇文章,你是打算孤獨終老?還是干點兒別的吧。

// As for why most problems are walked through by a mere sentence (even a single word), simply put, theyre just typical. I think a programmer with proper training should have adequate competence of code reading and adaptability to new languages. The given codes on Github are sufficient for the task of illustration. Minimal hints on the key idea is what I deem tolerable for a real interview. Beyond that, you fail. Without involvement of software engineering, design patterns or modular interface designs, a word-by-word explanation with illustration is totally tedious. Besides, a five-minute kill is not worth a twenty-minute autopsy. A post for every problem solved, serious? Looking to die alone playing Leetcode? There are better things to do for sure.

下面以題號為順序,對每一題的題意和解法做簡要描述:

// Following here is a summary of description and solution for every problem I solved:

LeetCode 簡略題解 - 1~100 - 知乎專欄

LeetCode 簡略題解 - 101~200 - 知乎專欄

LeetCode 簡略題解 - 201~300 - 知乎專欄

LeetCode 簡略題解 - 301~400 - 知乎專欄

LeetCode 簡略題解 - 401~500 - 知乎專欄

LeetCode 簡略題解 - 501~600 - 知乎專欄

LeetCode 簡略題解 - 601~700 - 知乎專欄

寫題解的目的只是用於備忘和加深理解,所以有沒有人看並不重要。

// Its just a memo to help fortify comprehension. Aint no big deal if nobodys reading this.

如果有人看了後,覺得我某道題的解法有值得討論或者錯誤的地方,歡迎在評論里指出。

// If anyone should feel the need for discussion, please leave your comment.

以下是更新日誌:

// Change logs are as follows:

截止2017年3月11日02:25,Leetcode的Algorithm部分(包括付費題目)總共491題。

// As of the moment 02:25, 2017.3.11, therere altogether 491 problems in the Algorithm section of Leetcode.

截止2017年3月23日05:02,Leetcode的Algorithm部分(包括付費題目)總共499題。

// As of the moment 05:02, 2017.3.23, therere altogether 499 problems in the Algorithm section of Leetcode.

截止2017年4月26日15:01,Leetcode的Algorithm部分(包括付費題目)總共519題。

// As of the moment 15:01, 2017.4.26, therere altogether 519 problems in the Algorithm section of Leetcode.

截止2017年5月3日11:13,Leetcode的Algorithm部分(包括付費題目)總共523題。

// As of the moment 11:13, 2017.5.3, therere altogether 523 problems in the Algorithm section of Leetcode.

截止2017年5月26日21:08,Leetcode的Algorithm部分(包括付費題目)總共535題。

// As of the moment 21:08, 2017.5.26, therere altogether 535 problems in the Algorithm section of Leetcode.

截止2017年6月10日16:52,Leetcode的Algorithm部分(包括付費題目)總共543題。

// As of the moment 16:52, 2017.6.10, therere altogether 543 problems in the Algorithm section of Leetcode.

截止2017年8月14日10:33,Leetcode的Algorithm部分(包括付費題目)總共585題。

// As of the moment 10:33, 2017.8.14, therere altogether 585 problems in the Algorithm section of Leetcode.


推薦閱讀:

一個與矩陣有關的演算法問題?
X、數據結構與演算法部分——分支
一把無限長的直尺,如何用最少的刻度將所有的整數長度(cm)都能畫出來?
如何證明Manacher演算法的時間複雜度是O(n)?

TAG:LeetCode | 算法与数据结构 | 计算机科学 |