LeetCode 簡略題解 - 1~100

#1 Two Sum

// #1 兩數之和

描述:給定一個整數數組和一個值target,求兩個下標i、j,使得a[i] + a[j] = target,返回下標。

// Description: Two Sum | LeetCode OJ

解法1:可以用哈希表不斷記錄每個元素對應的下標,然後查找target - a[i]就行了,查到了就有,查不到就沒有。時間空間複雜度都是O(N)。中規中矩。

// Solution 1: Use a hash table to record the index of every element and look for target - a[i] in the hash table. Positive, bingo; negative, keep looking. Time and space are both O(N). Will do.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:把數組排序,然後前後兩指針向中間靠攏。如果數組本身有序,此方法更好。時間O(N * logN),空間O(1)。

// Solution 2: Sort the array and let two pointers close in on each other from both ends. This works better when the array is already sorted. O(N * logN) time and O(1) space.

代碼2:太懶不寫

// Code 2: Too lazy to write.

#2 Add Two Numbers

// #2 兩數相加

描述:兩個大數,用鏈表表示。您給做個加法。

// Description: Add Two Numbers | LeetCode OJ

解法1:加唄。

// Solution 1: Just get it done.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#3 Longest Substring Without Repeating Characters

// #3 字母不帶重樣兒的最長子串

描述:給定一個字元串,看標題。

// Description: Longest Substring Without Repeating Characters | LeetCode OJ

解法1:用一個數組實時統計每個字母的出現次數,邊走邊數就行了。

// Solution 1: Maintain a counter array to keep the statistics of letters. Keep two pointers on watch and count as you go.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#4 Median of Two Sorted Arrays

// #4 倆有序數組的中位數

描述:給定倆有序數組,找出把它倆合併後所得數組的中位數。要求對數時間搞定。

// Description: Median of Two Sorted Arrays | LeetCode OJ

解法1:暴力歸併,O(N)時間。

// Solution 1: Brute-force merge, O(N) time.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:二分思想,遞歸實現。你要迭代也可以,不過代碼更複雜些就是了。然而這個解法不是O(logN),而是O(log ^ 2 N)時間。更好的解法我還沒想出來。

// Solution 2: Binary search solution by recursion. Iterative solution is OK, but requires more sophisticated measures. Yet this is not an O(logN), but an O(log ^ 2 N) solution. Im still working on a better one.

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

#5 Longest Palindromic Substring

// #5 最長迴文子串

描述:如題。

// Description: Longest Palindromic Substring | LeetCode OJ

解法1:暴力過。

// Solution 1: Brute-force.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:Manacher演算法,動態規劃的經典實例。

// Solution 2: Manacher Algorithm, one of most classical application of dynamic programming.

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

#6 ZigZag Conversion

// #6 字元串Z形排列

描述:給定字元串,按某種奇怪的方式排列一下。

// Description: ZigZag Conversion | LeetCode OJ

解法1:不涉及演算法,小心行事即可。

// Solution 1: Got nothing to do with algorthms, just beware of any bugs.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#7 Reverse Integer

// #7 反轉整數

描述:給定一個10進位整數,翻轉它。

// Description: Reverse Integer | LeetCode OJ

解法1:能用整數就別用浮點,能用數值就別用字元串。

// Solution 1: Make do with integer or numberic whenever possible. Extra type conversion means extra overhead.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#8 String to Integer (atoi)

// #8 字元串轉整數

描述:atoi,你懂的。

// Description: String to Integer (atoi) | LeetCode OJ

解法1:轉唄。

// Solution 1: Just do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#9 Palindrome Number

// #9 迴文數

描述:如題。

// Description: Palindrome Number | LeetCode OJ

解法1:跟迴文串一個道理。

// Solution 1: Same as palindromic strings.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#10 Regular Expression Matching

// #10 正則匹配

描述:如題。

// Description: Regular Expression Matching | LeetCode OJ

解法1:理解「回溯」二字。

// Solution 1 Try to understand the word "backtracking".

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#11 Container With Most Water

// #11 存水最多的容器

描述:給定一堆高矮不一的平行的牆,選擇其中兩堵作為兩壁,問最多能存多少水。

// Description: Container With Most Water | LeetCode OJ

解法1:O(N)演算法的思想很巧妙,需要一點推理。

// Solution 1: The O(N) solution is clever. Youll need a little reasoning to get ahold of it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#12 Integer to Roman

// #12 整數轉羅馬數字

描述:如題。

// Description: Integer to Roman | LeetCode OJ

解法1:模擬題。

// Solution 1: Simulation problem.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#13 Roman to Integer

// #13 羅馬數字轉整數

描述:如題。

// Description: Roman to Integer | LeetCode OJ

解法1:模擬題。

// Solution 1: Simulation problem.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#14 Longest Common Prefix

// #14 最長公共前綴

描述:給定一組字元串,找出最長公共前綴。

// Description: Longest Common Prefix | LeetCode OJ

解法1:找唄。

// Solution 1: Dont bother with trie or something.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#15 3Sum

// #15 三數之和

描述:給定一個數組和一個值target,找出所有三數加起來等於target的組合。

// Description: 3Sum | LeetCode OJ

解法1:將數組排序,一維度自由,剩下兩維度按Two Sum來搞。時間O(N ^ 2),空間O(1)。

// Solution 1: Sort the array. Set one dimension free and do Two Sum with the other two.O(N ^ 2) time and O(1) space.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#16 3Sum Closest

// #16 最接近的三數之和

描述:給定一個數組和一個值target,找出其中仨元素,使其加起來最接近target。

// Description: 3Sum Closest | LeetCode OJ

解法1:和3Sum思路類似,不過這次不斷更新結果即可。

// Solution 1: Similar to 3Sum, but this time we keep updating the result as we go.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#17 Letter Combinations of a Phone Number

// #17 手機鍵盤的字母組合

描述:Nokia手機,給定一個數字串,看看有多少種對應的字母串。

// Description: Letter Combinations of a Phone Number | LeetCode OJ

解法1:搜唄。

// Solution 1: Just make a search for it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#18 4Sum

// #18 四數之和

描述:不用多說了。

// Description: 4Sum | LeetCode OJ

解法1:兩維度自由,兩維度Two Sum。當然,該排序排序。

// Solution 1: Set two dimensions free, do a Two Sum on the other two. Sort the array if you have to.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#19 Remove Nth Node From End of List

// #19 刪除鏈表倒數第N個節點

描述:如題。

// Description: Remove Nth Node From End of List | LeetCode OJ

解法1:鏈表題,小心駛得萬年船。

// Solution 1: Cant be too careful with a linked list.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#20 Valid Parentheses

// #20 有效括弧序列

描述:給定一個括弧序列,判斷其是否合法。

// Description: Valid Parentheses | LeetCode OJ

解法1:用了個棧。

// Solution 1: Use a stack.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#21 Merge Two Sorted Lists

// #21 歸併兩個有序鏈表

描述:如題。

// Description: Merge Two Sorted Lists | LeetCode OJ

解法1:只要是鏈表題,就要極力避免new新節點。你可以從操作系統的角度去考慮一次new操作背後發生的事情,尤其是內核態的部分。知其然,弗若知其所以然。

// Solution 1: Avoid using 「new」 whenever dealing with a linked list problem. Think about the things behind a "new" operation, especially the kernel-space part. It takes more to get to the "how" and "why" than a mere "what".

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#22 Generate Parentheses

// #22 生成括弧。

描述:給定一個長度,生成所有此長度的合法括弧序列。

// Description: Generate Parentheses | LeetCode OJ

解法1:Catalan數知道吧?搜。

// Solution 1: Ever heard of the Catalan Numbers? Just DFS.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#23 Merge k Sorted Lists

// #23 歸併k個有序鏈表

描述:如題。

// Description: Merge k Sorted Lists | LeetCode OJ

解法1:快使用最小堆,紅紅火火。

// Solution 1: Yo, Min heap. Check it out, dogg.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#24 Swap Nodes in Pairs

// #24 交換節點對

描述:給定一個鏈表,每兩個節點做一下交換。

// Description: Swap Nodes in Pairs | LeetCode OJ

解法1:換唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#25 Reverse Nodes in k-Group

// #25 每k個節點反轉一下

描述:給定一個鏈表,每k個節點一組,做一次反轉。尾巴湊不齊的,自己看著辦。

// Description: Reverse Nodes in k-Group | LeetCode OJ

解法1:很容易出bug的題,小心行事。把常用操作單獨寫成函數是個好習慣。強行壓縮代碼長度,遲早是要栽跟頭的。

// Solution 1: A tricky problem, proceed with caution. Its good practice to wrap what you do frequently in a function. Excessive obsession with shortest code length is gonna bring you down, one of these days.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#26 Remove Duplicates from Sorted Array

// #26 去除有序數組的重複元素

描述:如題。

// Description: Remove Duplicates from Sorted Array | LeetCode OJ

解法1:水題。以後會經常寫這個的,就跟打字一樣。

// Solution 1: Piece of cake, youll be seeing a lot of this in the future.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#27 Remove Element

// #27 刪除元素

描述:刪除數組中所有等於某個值的元素。

// Description: Remove Element | LeetCode OJ

解法1:刪唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#28 Implement strStr()

// #28 實現strStr()

描述:字元串匹配,經典問題。

// Description: Implement strStr() | LeetCode OJ

解法1:暴力過,O(N ^ 2)時間。

// Solution 1: Brute-force solution with O(N ^ 2) time.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:Rabin-Karp演算法。對字元串做哈希是一種非常巧妙的做法,這種思想在很多情景下可以大大提高匹配效率。有必要的話,可以複查以嚴格保證匹配的正確性。

// Solution 2: Rabin-Karp Algorithm. Hashing a string is clever way to immensely accelerate pattern matching. Double check if you cant tolerate false positives.

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

解法3:傳說中的KMP演算法,一個我寫了不下十次還是很難獨立一次性寫對的演算法。next數組堪稱藝術品,很清真。

// Solution 3: The legendary KMP Algorithm, which I cant possibly write down perfectly even after using a dozen of it. The "next" array is simply a great work of art, as beautiful as it is powerful.

代碼3:github.com/zhuli1990110

// Code 3: github.com/zhuli1990110

#29 Divide Two Integers

// #29 兩整數相除

描述:不準乘除,不準膜。

// Description: Divide Two Integers | LeetCode OJ

解法1:加減配合移位。照搞不誤。

// Solution 1: +-&|<<, nomsayin?

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#30 Substring with Concatenation of All Words

// #30 由所有單詞連成的子串

描述:給定一個無重複單詞的字典D,和一個長字元串S。找出S中的子串,該子串恰好是D中所有單詞連接而成。

// Description: Substring with Concatenation of All Words | LeetCode OJ

解法1:碰見這種字典多而短的問題,本能地會想到字典樹。其實用也可以,不用也可以。反正都要搜索一番。然而我閑字典樹麻煩,所以想到用哈希值來代替字元串,這樣能夠O(1)時間進行匹配。結果確實很不錯。哈希大法好,但有一點要記住:雖然哈希碰撞很難發生,但墨菲定律在那兒擺著,不檢查的話遲早要完。

// Solution 1: Its intuitive to think of trie when dealing with a large dictionary of short words. Use it or not, youll figure out a solution yourself. I chose string hashing to accelerate my string matching, which worked out just fine. Hashing it powerful, but keep Murphys Law in mind, that hashing collisons do happen. You dont do a double check, your call.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#31 Next Permutation

// #31 下一個排列

描述:給定一個排列,按照字典序把下一個弄出來。

// Description: Next Permutation | LeetCode OJ

解法1:O(N)時間,注意要從後往前找。

// Solution 1: O(N) time. Search from back to front.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#32 Longest Valid Parentheses

// #32 最長合法括弧子串

描述:給定一個括弧序列,找出最長的合法括弧子串。

// Description: Longest Valid Parentheses | LeetCode OJ

解法1:碰見這種子串問題,基本都是一前一後兩個指針,不過寫法倒是各有不同。看代碼。

// Solution 1: These "substring" problems can usually be solved with two running pointers. Although they come in various forms. Find more in the code.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#33 Search in Rotated Sorted Array

// #33 旋轉過有序數組的查找問題

描述:一個有序數組,可能進行了循環移位,在裡面進行查找。

// Description: Search in Rotated Sorted Array | LeetCode OJ

解法1:先把旋轉的偏移量算出來,再二分查找。兩部分都是O(logN),所以還行。

// Solution 1: Find the rotation offset and do an offsetted binary search. Both parts are O(logN), works for me.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#34 Search for a Range

// #34 找區間。

描述:給定一個有序數組,找出某個值的起始和終止區間。

// Description: Search for a Range | LeetCode OJ

解法1:lower_bound和upper_bound一起用。

// Solution 1: lower_bound and upper_bound will suffice.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#35 Search Insert Position

// #35 尋找插入位置

描述:給定有序數組和一個值,尋找合適的插入位置。

// Description: Search Insert Position | LeetCode OJ

解法1:二分。

// Solution 1: Binary search.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#36 Valid Sudoku

// #36 檢查數獨

描述:如題。

// Description: Valid Sudoku | LeetCode OJ

解法1:按規矩來唄。

// Solution 1: Routine.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#37 Sudoku Solver

// #37 解數獨

描述:如題。

// Description: Sudoku Solver | LeetCode OJ

解法1:標準遞歸回溯問題,注意遞歸寫法,不要增加不必要的複雜度。跳舞鏈神馬的,還是算了吧,太複雜。

// Solution 1: A typical DFS + backtracking problem. Write it carefully though, dont incur unnecessary time complexity. As for Dancing Links, Ill forget about it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#38 Count and Say

// #38 數數

描述:給定一個數字串,讀出來。並把讀的方法也表示成一個數字串。如此計算出第N個串。

// Description: Count and Say | LeetCode OJ

解法1:不知道有沒有什麼數學解法?

// Solution 1: Is there any analytical solution? I mean, mathematically?

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#39 Combination Sum

// #39 組合之和

描述:給定一個集合以及一個值target,找出所有加起來等於target的組合,要不帶重樣兒的。每個元素可以用無數次。

// Description: Combination Sum | LeetCode OJ

解法1:搜。

// Solution 1: DFS.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#40 Combination Sum II

// #40 組合之和2

描述:和之前差不多,區別是每個元素只能用一次。

// Description: Combination Sum II | LeetCode OJ

解法1:搜。

// Solution 1: DFS.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#41 First Missing Positive

// #41 第一個未出現的正數

描述:給定無序數組,找出第一個未出現的正整數。

// Description: First Missing Positive | LeetCode OJ

解法1:數組本身就可以用於標記。

// Solution 1: The input array itself can be used as marker array.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#42 Trapping Rain Water

// #42 雨水積水問題

描述:給定一個地形,計算能存多少雨水。

// Description: Trapping Rain Water | LeetCode OJ

解法1:不要考慮一個水坑能存多少水,而要考慮每一條能存多少雨水,這種違背直觀的思考方式反而更容易編程。

// Solution 1: Dont try to calculate how much water can be trapped in a water pit. Think about how much water can be stored in each "stripe". This counter-intuitive way of thinking is actually better suited for programming.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#43 Multiply Strings

// #43 高精度乘法

描述:如題。

// Description: github.com/zhuli1990110

解法1:暴力。FFT解法我沒寫。

// Solution 1: Brute-force. I didnt write the FFT solution myself, too much trouble.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#44 Wildcard Matching

// #44 通配符

描述:模式匹配的另一個基礎示例。

// Description: Wildcard Matching | LeetCode OJ

解法1:還是回溯。值得注意的是如何想辦法剪枝。

// Solution 1: Use backtracking. How to do the pruning is one of the things worth noting.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#45 Jump Game II

// #45 青蛙跳2

描述:和Jump Game一樣,這次計算最小步數。

// Description: Jump Game II | LeetCode OJ

解法1: 掃一遍就行了。

// Solution 1: Sweep it over.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#46 Permutations

// #46 全排列

描述:列印全排列。

// Description: Permutations | LeetCode OJ

解法1:用next_permutation迭代。

// Solution 1: Iterate with next_permutation.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:DFS。

// Solution 2: DFS.

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

#47 Permutations II

// #47 全排列2

描述:和Permutations一樣,不過序列可能有重複元素。

// Description: Permutations II | LeetCode OJ

解法1:用next_permutation迭代,不過函數自己寫。

// Solution 1: Iterate with next_permutation, but write your own this time.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#48 Rotate Image

// #48 旋轉圖像

描述:順時針旋轉90度,盡量不用額外空間。

// Description: Rotate Image | LeetCode OJ

解法1:矩陣分四塊兒就行。輪換著賦值,可保證O(1)空間。

// Solution 1: Segment the matrix into 2 by 2. By doing the value reassignments in a rotated manner, O(1) space is thus achieved.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#49 Group Anagrams

// #49 變位詞歸類

描述:給定一堆單詞,把變位詞放一塊兒去。

// Description: Group Anagrams | LeetCode OJ

解法1:變位詞排序之後就一樣了。

// Solution 1: Anagrams become one after sorting.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#50 Pow(x, n)

// #50 冪

描述:求冪。

// Description: Pow(x, n) | LeetCode OJ

解法1:快速冪。

// Solution 1: Binary exponentiation.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#51 N-Queens

// #51 N皇后

描述:NxN棋盤擺N個棋子,要求不能同行、同列、同對角線、同反對角線,返回所有擺法。

// Description: N-Queens | LeetCode OJ

解法1:DFS + 回溯.

// Solution 1: DFS + backtracking.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#52 N-Queens II

// #52 N-Queens II

描述:N皇后問題,輸出解的個數。

// Description: N-Queens II | LeetCode OJ

解法1:目前並沒有數學解,所以還是搜。

// Solution 1: Theres currently no known formula for N-queen problem. So well do it the old way.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#53 Maximum Subarray

// #53 最大子數組和

描述:如題。

// Description: Maximum Subarray | LeetCode OJ

解法1:教材經典例子。

// Solution 1: Algo 101 on the textbook.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#54 Spiral Matrix

// #54 螺旋矩陣

描述:按螺旋方式遍歷矩陣。

// Description: Spiral Matrix | LeetCode OJ

解法1:模擬題。

// Solution 1: Simulation problem.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#55 Jump Game

// #55 青蛙跳

描述:給定一個數組A,每個A[i]表示你最遠能跳的距離。從0出發問是否能跳到終點。

// Description: Jump Game | LeetCode OJ

解法1:掃一遍就行了。

// Solution 1: Jump ahead, dont look back.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#56 Merge Intervals

// #56 歸併區間

描述:給定一些區間,該合併合併。

// Description: Merge Intervals | LeetCode OJ

解法1:排個序,歸併。

// Solution 1: Sort and merge.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#57 Insert Interval

// #57 插入區間

描述:給定一堆區間,插入一個新區間。

// Description: Insert Interval | LeetCode OJ

解法1:跟Merge Intervals一個思路。

// Solution 1: Similar to Merge Intervals.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#58 Length of Last Word

// #58 最後一個詞的長度

描述:如題。

// Description: Length of Last Word | LeetCode OJ

解法1:無聊。

// Solution 1: Boring.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#59 Spiral Matrix II

// #59 旋轉矩陣2

描述:把1之n ^ 2按照螺旋順序填入方陣。

// Description: Spiral Matrix II | LeetCode OJ

解法1:模擬題。

// Solution 1: Simulation problem.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#60 Permutation Sequence

// #60 排列序列

描述:1~n有n!個排列,找出其中字典序排第k的排列。

// Description: Permutation Sequence | LeetCode OJ

解法1:看代碼吧。

// Solution 1: Just read the code.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#61 Rotate List

// #61 旋轉鏈表

描述:給定一個鏈表, 循環移位k次。

// Description: Rotate List | LeetCode OJ

解法1:要面面俱到。

// Solution 1: Mind the edge cases.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#62 Unique Paths

// #62 不同路徑

描述: 給定MxN棋盤,只允許從左上往右下走,每次走一格。共有多少種走法?

// Description: Unique Paths | LeetCode OJ

解法1:排列組合。

// Solution 1: Combination.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#63 Unique Paths II

// #63 不同路徑2

描述:和之前一樣,不過這次有些格子不讓走。

// Description: Same as before, only in that some grids are blocked.

解法1:動態規劃。

// Solution 1: Dynamic programming.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#64 Minimum Path Sum

// #64 最小路徑和

描述:和Unique Paths一樣,不過這次每個點有權值,求最小路徑和。

// Description: Same as Unique Paths, but this time we assign a weight to each grid and calculate minimum weight sum for the path.

解法1:DP。

// Solution 1: DP.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#65 Valid Number

// #65 數值判斷

描述:給定一個字元串,判斷是否符合數值類型的格式。

// Description: Valid Number | LeetCode OJ

解法1:典型的面試題,考察思維是否足夠縝密。照理說一個正則表達式可以搞定,不過編譯直接超時了,說明伺服器對這個做了限制。所以只得手寫各種判斷。此題無須奇技淫巧,惟「認真」二字足矣。

// Solution 1: A typical filter for discreet thinkers. I thought a regular expression would suffice, but the compilation simply timed out. It appears the OJ servers discourage such attempt. Som no fancy tricks needed, just think it through and do it carefully.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#66 Plus One

// #66 加一

描述:高精度加一。

// Description: Plus One | LeetCode OJ

解法1:加唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#67 Add Binary

// #67 二進位加法

描述:二進位高精度加法。

// Description: Add Binary | LeetCode OJ

解法1:加唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#68 Text Justification

// #68 文字對齊

描述:給定一列單詞,按照指定寬度將其排成格式工整的洋文。

// Description: Text Justification | LeetCode OJ

解法1:注意邊界條件。

// Solution 1: Edge cases.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#69 Sqrt(x)

// #69 開方

描述:整數開方

// Description: Sqrt(x) | LeetCode OJ

解法1:二分吧,你要牛頓法也行。

// Solution 1: Binary search will do. Newton-Raphson method is OK but a bit overpowered.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#70 Climbing Stairs

// #70 爬樓梯

描述:爬樓梯,每次能往上爬一或兩級,問總共有多少種爬法。

// Description: Climbing Stairs | LeetCode OJ

解法1:斐波那契。

// Solution 1: Fibonacci.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#71 Simplify Path

// #71 簡化路徑

描述:如題。

// Description: Simplify Path | LeetCode OJ

解法1:棧。

// Solution 1: Stack.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#72 Edit Distance

// #72 編輯距離

描述:最短編輯距離,也叫Levenshtein距離。

// Description: Edit Distance | LeetCode OJ

解法1:動態規劃經典問題。

// Solution 1: A classic DP problem.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#73 Set Matrix Zeroes

// #73 矩陣置0

描述:給定一個矩陣,只要某個元素為0,就把對應的整行整列都置0。

// Description: Set Matrix Zeroes | LeetCode OJ

解法1:巧妙的地方在於如何避免外開闢空間,看代碼吧。

// Solution 1: The tricky part is to do it in-place. Read the code for more detail.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#74 Search a 2D Matrix

// #74 二維矩陣查找

描述:給定一個二維數組,balabala。

// Description: Search a 2D Matrix | LeetCode OJ

解法1:說白了,就是個一維有序數組。

// Solution 1: Expand it into an 1-d array, youll see.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#75 Sort Colors

// #75 顏色排序

描述:給定三中間色,分別標號為012, 按這個標號排序。

// Description:

解法1:這種題除了用來面試,簡直狗屁不通。正所謂千年科舉,牢籠人才。天下讀書人,一門心思都給了八股制藝,倒沒幾個人去富國強兵、振興科教。這種為出題而出題的腦殘面試題,豈不如出一轍?實乃可笑!你喜歡出這種賣弄奇技淫巧的面試題,我就如你所願,寫這麼個「聰明」的解法。你說把0、1、2的個數都數出來不就得了?我偏要玩兒出個花兒來。否則,怎麼顯示我琢磨面試題下的苦功?倘若天下人都一門心思琢磨怎麼考試,這國家也就藥丸了。

// Solution 1: F* this problem.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#76 Minimum Window Substring

// #76 最小窗口子串

描述:給定一個長串S和一個短串T。求S中包含T所有字母的最短字串。

// Description: Minimum Window Substring | LeetCode OJ

解法1:凡是這種滑動窗口的問題,都是兩個指針一前一後,你追我趕。

// Solution 1: All such "sliding window" problems can be tackled with two running pointers, one after the other.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#77 Combinations

// #77 組合

描述:從1~N里選K個數,輸出所有選法。

// Description: Combinations | LeetCode OJ

解法1:DFS。

// Solution 1: DFS.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:類似next_permutation。

// Solution 2: Similar to next_permutation.

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

#78 Subsets

// #78 子集

描述:冪集。

// Description: Subsets | LeetCode OJ

解法1:DFS。

// Solution 1: DFS.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:冪集中的元素和0~2 ^ n - 1可構成雙射。

// Solution 2: There exists a bijection between the power set and {0, 1, ..., 2 ^ n - 1}, where n is the cardinality of the set S.

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

#79 Word Search

// #79 找單詞

描述:給定一個字母矩陣,允許從任一點出發四處走。看軌跡能否構成一個單詞。

// Description: Word Search | LeetCode OJ

解法1:DFS。

// Solution 1: DFS.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#80 Remove Duplicates from Sorted Array II

// #80 有序數組去重2

描述:和之前一樣,不過這次允許元素重複一次。

// Description: Remove Duplicates from Sorted Array II | LeetCode OJ

解法1:小把戲。

// Solution 1: Small trick.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#81 Search in Rotated Sorted Array II

// #81 有序旋轉數組查找2

描述:和之前一樣,不過這次數組裡可能有重複元素。

// Description: Search in Rotated Sorted Array II | LeetCode OJ

解法1:對於相等元素,必須跳過,否則無從得知,這有序的部分究竟在重複元素的左側還是右側。所以時間複雜度最壞可以達到O(N)。

// Solution 1: The duplicates must be skipped one by one, or else theres no way to know which way to go during the binary search. Thus the worst-case time complexity can be O(N).

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#82 Remove Duplicates from Sorted List II

// #82 有序鏈表去重2

描述:給定有序鏈表,凡有重複元素者,盡數去除。

// Description: Remove Duplicates from Sorted List II | LeetCode OJ

解法1:去唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#83 Remove Duplicates from Sorted List

// #83 有序鏈表去重

描述:和之前一樣,這次只去掉重複部分。

// Description: Remove Duplicates from Sorted List | LeetCode OJ

解法1:去唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#84 Largest Rectangle in Histogram

// #84 直方圖中的最大矩形

描述:給定一個直方圖,求其能覆蓋的最大矩形。

// Description: Largest Rectangle in Histogram | LeetCode OJ

解法1:和Trapping Rain Water那題一樣,從每一條的角度來考慮問題。看向左向右各能延伸多長,用DP可以O(N)時間解決。

// Solution 1: Same as Trapping Rain Water, consider the problem from each single stripe. Calculate the left and right boundary for the stripes using DP in O(N) time, then we have the answer.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#85 Maximal Rectangle

// #85 最大矩形

描述:給定一個01矩陣,找出其中全由1構成的最大矩形。

// Description: Maximal Rectangle | LeetCode OJ

解法1:這題有意思,可以利用Largest Rectangle in Histogram那題的思路來做,請看代碼。把一個問題化歸為另一個問題,是復用代碼的常見手段。

// Solution 1: An interesting problem. We can use Largest Rectangle in Histogram to solve this one. Please read the code. Its common practice to convert one problem to another, so that we can reuse existing code.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#86 Partition List

// #86 劃分鏈表

描述:給定一個鏈表和一個值,把小於等於和大於該值的部分分別放到鏈表的一前一後去。

// Description:

解法1:分為兩個鏈表,然後合併即可。

// Solution 1: Split in two and concatenate as one.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#87 Scramble String

// #87 亂序字元串

描述:給定一個字元串S,允許遞歸地將其分為左右兩部分並調換位置,稱其為「亂序」。現給定兩字元串,判斷其是否互為「亂序」。

// Description: Scramble String | LeetCode OJ

解法1:DP。不知道有沒有O(N^3)時間的解法?

// Solution 1: DP. I wonder if theres an O(N ^ 3) solution?

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#88 Merge Sorted Array

// #88 歸併有序數組

描述:給定有序數組a1和a2,把a2歸併到a1中。

// Description: Merge Sorted Array | LeetCode OJ

解法1:並唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#89 Gray Code

// #89 格雷碼

描述:生成N位格雷碼。

// Description: Gray Code | LeetCode OJ

解法1:想想N - 1位格雷碼和N位格雷碼的關係。

// Solution 1: Think about the relationship between (N - 1)-bit gray code and N-bit gray code.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#90 Subsets II

// #90 子集2

描述:還是冪集,不過這次是多重集的冪集。

// Description: Subsets II | LeetCode OJ

解法1:因為是多重集,所以這次咱還是搜吧。其實雙射還是存在的,不過代碼沒那麼好寫了,所以我懶得寫。

// Solution 1: Id prefer DFS this time. Although the bijection still exists, the code isnt as easy to write. Never mind.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#91 Decode Ways

// #91 解碼方式

描述:將A~Z和1~26一一映射,現在給定一個數字串,請計算有多少種不同的解碼方式。

// Description: Decode Ways | LeetCode OJ

解法1:這個可以DP。

// Solution 1: DP will do.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#92 Reverse Linked List II

// #92 反轉鏈表2

描述:給定一個鏈表,反轉第m~n個節點。

// Description: Reverse Linked List II | LeetCode OJ

解法1:分前中後三部分完成。

// Solution 1: Split in three.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#93 Restore IP Addresses

// #93 還原IP地址

描述:給定一個沒加點的IP地址,返回所有可能的合法IP地址。

// Description: Restore IP Addresses | LeetCode OJ

解法1:加唄。

// Solution 1: Do it.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#94 Binary Tree Inorder Traversal

// #94 二叉樹中序遍歷

描述:如題。

// Description: Binary Tree Inorder Traversal | LeetCode OJ

解法1:遞歸。

// Solution 1: Recursive.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:迭代。

// Solution 2: Iterative.

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

#95 Unique Binary Search Trees II

// #95 不同二叉搜索樹2

描述:輸出中序遍歷是{1, 2, ..., n}的所有二叉搜索樹。

// Description: Unique Binary Search Trees II | LeetCode OJ

解法1:DFS。

// Solution 1: DFS.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#96 Unique Binary Search Trees

// #96 不同二叉搜索樹

描述:輸出中序遍歷是{1, 2, ..., n}的所有二叉搜索樹個數。

// Description: Unique Binary Search Trees | LeetCode OJ

解法1:Catalan數。為什麼呢?自己想。

// Solution 1: Catalan Number.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#97 Interleaving String

// #97 交叉字元串

描述:給定仨字元串S1、S2、S3,判斷S3是否可以由S1和S2交叉而成。

// Description: Interleaving String | LeetCode OJ

解法1:DP。

// Solution 1: DP.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

#98 Validate Binary Search Tree

// #98 驗證二叉搜索樹

描述:給定二叉樹,判斷是否為二叉搜索樹。

// Description: Validate Binary Search Tree | LeetCode OJ

解法1:針對「有序」二字做文章即可。

// Solution 1: Dig around the word 「order」.

代碼1:github.com/zhuli1990110

// Code 1: https://github.com/zhuli19901106/leetcode-2/blob/master/validate-binary-search-tree_1_AC.cpp

#99 Recover Binary Search Tree

// #99 恢復二叉搜索樹

描述:二叉搜索樹中的兩個節點被調換了,麻煩給調回來。

// Description: Recover Binary Search Tree | LeetCode OJ

解法1:執行中序遍歷,找出兩個節點。注意邊界情況。

// Solution 1: Perform an inorder traversal to locate target nodes. Watch for edge cases.

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110

解法2:應出題者裝逼要求,強行寫了個Morris中序遍歷。難道現在面試都要求會寫Morris遍歷了?真乃世道艱危,國將不國啊。你敢再讓寫一遍這破玩意兒,我豬某寧死也要投降。

// Solution 2: Is this showing off or zhuangbi?

代碼2:github.com/zhuli1990110

// Code 2: github.com/zhuli1990110

#100 Same Tree

// #100 同一棵樹

描述:同一個夢想。

// Description: Same Tree | LeetCode OJ

解法1:遞歸或者迭代,甚至序列化都行,烯烴尊便。

// Solution 1:

代碼1:github.com/zhuli1990110

// Code 1: github.com/zhuli1990110


推薦閱讀:

007 Reverse Integer[E]
leetcode-1
[leetcode algorithms]題目18
LeetCode 簡略題解 - 201~300
008 String to Integer (atoi)[M]

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