1. Two Sum
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
solution 1: brute force
solution 2: two-pass hash table
solution 3: one-pass hash table
2. Add Two Numbers
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
solution: elementary math
Follow up
What if the the digits in the linked list are stored in non-reversed order? For example:
(3→4→2)+(4→6→5)=8→0→7
思路,pass 3次,第1次比较长短,第2次相加做出正向和反向2个链表,第3次用反向链表做进位,不知道是不是这样,做了也无法用LC检查,就先不浪费时间实现之了。
146. LRU Cache
Approach 1: Ordered dictionary,java和python有相应的类型
Approach 2: Hashmap + DoubleLinkedList
5. Longest Palindromic Substring
Approach 1: Longest Common Substring,正序和倒序的common substring就是palindromic的吗?不是的,所以需要修正。Matt: 找common substring的算法?复杂度?
Approach 2: Brute Force,判断所有子串,显然不可取
Approach 3: Dynamic Programming
Approach 4: Expand Around Center,我的本能直觉想法是这个(见我的第1个和第2个正确的submission),不过没有solution里想得清楚,所以实现得比较啰嗦,第3个正确的submission按照solution简化了一下。
Approach 5: Manacher's Algorithm, there is even an O(n)O(n) algorithm called Manacher's algorithm, explained here in detail. However, it is a non-trivial algorithm, and no one expects you to come up with this algorithm in a 45 minutes coding session.
4. Median of Two Sorted Arrays
我的本能直觉想法其实实现的就还可以,就是2个index,一起比较着向后挪,注意一个数组先终结的情况。
Solution的解释很繁琐,但是借鉴了它的想法,即既然是找median则确定在数组1中的index,在数组2中的index就也确定了,比较数组1和数组2的下一个元素,可知是不是满足大小关系,试着这么做了,可以,但是坑太多了,各种边缘情况。