diff --git a/001 Two Sum.py b/001 Two Sum.py index 88e5548..b931407 100644 --- a/001 Two Sum.py +++ b/001 Two Sum.py @@ -9,16 +9,12 @@ Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2 """ -__author__ = 'Danyang' class Solution: def twoSum_TLE(self, num, target): """ built-in method .index - :param num: list - :param target: int - :return: tuple, (index1, index2) """ nums = num for ind1, val in enumerate(nums): @@ -37,10 +33,6 @@ def twoSum_TLE_2(self, num, target): def twoSum(self, num, target): """ Hash Map - - :param num: list - :param target: int - :return: tuple, (index1, index2) """ hash_map = {} for ind, val in enumerate(num): @@ -54,4 +46,4 @@ def twoSum(self, num, target): if __name__=="__main__": - print Solution().twoSum([3, 2, 4], 6) \ No newline at end of file + print(Solution().twoSum([3, 2, 4], 6)) \ No newline at end of file diff --git a/004 Add Two Numbers.py b/002 Add Two Numbers.py similarity index 92% rename from 004 Add Two Numbers.py rename to 002 Add Two Numbers.py index dcc6364..5ff1d77 100644 --- a/004 Add Two Numbers.py +++ b/002 Add Two Numbers.py @@ -76,4 +76,5 @@ def addNode(self, node1, node2): l1s[i].next = l1s[i+1] for i in range(len(l2s)-1): l2s[i].next = l2s[i+1] - Solution().addTwoNumbers(l1s[0], l2s[0]) + solution = Solution().addTwoNumbers(l1s[0], l2s[0]) + print(solution) diff --git a/003 Longest Substring Without Repeating Characters.py b/003 Longest Substring Without Repeating Characters.py index c9c5747..df8dd83 100644 --- a/003 Longest Substring Without Repeating Characters.py +++ b/003 Longest Substring Without Repeating Characters.py @@ -36,4 +36,6 @@ def lengthOfLongestSubstring(self, s): visited_last_index[ord(val)] = ind # update last visited index - return longest \ No newline at end of file + return longest + +print(Solution().lengthOfLongestSubstring("abcabcbb")) \ No newline at end of file diff --git a/002 Median of Two Sorted Arrays.py b/004 Median of Two Sorted Arrays.py similarity index 71% rename from 002 Median of Two Sorted Arrays.py rename to 004 Median of Two Sorted Arrays.py index 8aebcfa..efd30bd 100644 --- a/002 Median of Two Sorted Arrays.py +++ b/004 Median of Two Sorted Arrays.py @@ -1,8 +1,7 @@ """ -There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall -run time complexity should be O(log (m+n)). +There are two sorted arrays A and B of size m and n respectively. +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). """ -__author__ = 'Danyang' class Solution: @@ -23,10 +22,6 @@ def findMedianSortedArrays(self, A, B): T(N) = T(3/4 N) + O(1), thus T(N) = O(lg N) where N = |A|+|B| O(log (m+n)), thus binary search. - - :param A: list - :param B: list - :return: float """ m = len(A) n = len(B) @@ -36,13 +31,6 @@ def findMedianSortedArrays(self, A, B): return self.find_kth(A, B, (m+n)/2) def find_kth(self, A, B, k): - """ - - :param A: - :param B: - :param k: index starting from 0 - :return: - """ if not A: return B[k] if not B: return A[k] if k == 0: return min(A[0], B[0]) @@ -59,7 +47,7 @@ def find_kth(self, A, B, k): if __name__ == "__main__": - assert Solution().findMedianSortedArrays([1, 2], [1, 2, 3]) == 2 - assert Solution().findMedianSortedArrays([1, 2], [3]) == 2 - assert Solution().findMedianSortedArrays([1], [2, 3]) == 2 - assert Solution().findMedianSortedArrays([1, 2], [1, 2]) == 1.5 + print(Solution().findMedianSortedArrays([1, 2], [1, 2, 3])) # == 2) + print(Solution().findMedianSortedArrays([1, 2], [3])) # == 2) + print(Solution().findMedianSortedArrays([1], [2, 3])) # == 2) + print(Solution().findMedianSortedArrays([1, 2], [1, 2])) # == 1.5) diff --git a/014 3Sum.py b/014 3Sum.py index 3100ae5..096df14 100644 --- a/014 3Sum.py +++ b/014 3Sum.py @@ -126,4 +126,4 @@ def threeSum(self, num): if __name__ == "__main__": - print Solution().threeSum([-1, 0, 1, 2, -1, -4]) \ No newline at end of file + print(Solution().threeSum([-1, 0, 1, 2, -1, -4])) \ No newline at end of file diff --git a/018 4Sum.py b/018 4Sum.py index a435d05..ee2ea1d 100644 --- a/018 4Sum.py +++ b/018 4Sum.py @@ -80,16 +80,16 @@ def fourSum(self, num, target): return [] num.sort() - for p in xrange(length): - for q in xrange(p+1, length): + for p in range(length): + for q in range(p+1, length): # record the pair sum if num[p]+num[q] not in sum2index: sum2index[num[p]+num[q]] = [(p, q)] else: sum2index[num[p]+num[q]].append((p, q)) - for i in xrange(length): - for j in xrange(i+1, length-2): + for i in range(length): + for j in range(i+1, length-2): sum_remain = target-num[i]-num[j] if sum_remain in sum2index: # construct the result @@ -97,4 +97,6 @@ def fourSum(self, num, target): if pair[0]>j: # avoid duplicate result_set.add(( num[i], num[j], num[pair[0]], num[pair[1]] )) - return [list(i) for i in result_set] # convert tuple to list \ No newline at end of file + return [list(i) for i in result_set] # convert tuple to list + +print(Solution().fourSum([1, 0, -1, 0, -2, 2], 0)) \ No newline at end of file diff --git a/121 Best Time to Buy and Sell Stock II.py b/121 Best Time to Buy and Sell Stock II.py index 63c84bb..c42313d 100644 --- a/121 Best Time to Buy and Sell Stock II.py +++ b/121 Best Time to Buy and Sell Stock II.py @@ -5,7 +5,6 @@ one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). """ -__author__ = 'Danyang' class Solution: diff --git a/122 Best Time to Buy and Sell Stock.py b/122 Best Time to Buy and Sell Stock.py index 7f8fe2f..b3f5ee8 100644 --- a/122 Best Time to Buy and Sell Stock.py +++ b/122 Best Time to Buy and Sell Stock.py @@ -56,4 +56,4 @@ def maxProfitDelta(self, prices): if __name__ == "__main__": - assert Solution().maxProfit([3, 2, 1, 4, 5, 6, 2]) == 5 \ No newline at end of file + print(Solution().maxProfit([3, 2, 3, 4, 5, 6, 1])) #a == 5 \ No newline at end of file diff --git a/146 LRU Cache.py b/146 LRU Cache.py index 62ed86e..b786a61 100644 --- a/146 LRU Cache.py +++ b/146 LRU Cache.py @@ -106,10 +106,10 @@ def set(self, key, value): :param value: int :return: value """ - if key in self.dic: + if key in self.dic: # 可以替换位置,或者删除,再添加 self.q.remove(key) self.q.insert(0, key) - else: + else: # 直接插入,并删除尾部 if len(self.q)+1 <= self.capacity: self.q.insert(0, key) else: @@ -117,3 +117,19 @@ def set(self, key, value): self.q.insert(0, key) self.dic[key] = value + + +def test(): + cache = LRUCache(2) + cache.set(1, 1) + cache.set(2, 2) + print(cache.get(1)) + cache.set(3, 3) # evicts key 2 + print(cache.get(2)) # returns -1 (not found) + cache.set(4, 4) # evicts key 1 + print(cache.get(1)) # returns -1 (not found) + print(cache.get(3)) # returns 3 + print(cache.get(4)) # returns 4 + + +test() \ No newline at end of file