LeetCode 有关栈、堆、队列的做题笔记 Python实现
2019-05-07 本文已影响0人
划水型派大星
有关栈、堆、队列的LeetCode做题笔记,Python实现
20. 有效的括号 Valid Parentheses
使用 Stack 栈 来操作,用了一个技巧是先做一个字典,key
为右括号,value
为左括号。
class Solution:
def isValid(self, s: str) -> bool:
stack = []
mapping = {')':'(', '}':'{', ']':'['}
for c in s:
if c not in mapping:
stack.append(c)
elif not stack or mapping[c] != stack.pop():
return False
return not stack
703. 数据流中的第K大元素 Kth Largest Element in a Stream
方法一:直接降序排序,然后取第k个元素返回,add时每次都再排序一次,这样时间复杂度为O(k*logk)
# 1.直接排序
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.nums = nums
self.k = k
self.nums.sort(reverse = True)
while len(self.nums) > k:
self.nums.pop()
def add(self, val: int) -> int:
self.nums.append(val)
self.nums.sort(reverse = True)
if len(self.nums) > self.k:
self.nums.pop()
return self.nums[-1]
方法二:使用小顶堆实现的优先队列,Python 中标准库 heapq 就是小顶堆,时间复杂度降低为O(k)
# 2.小顶堆
import heapq
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.pool = nums
heapq.heapify(self.pool)
self.k = k
while len(self.pool) > k:
heapq.heappop(self.pool)
def add(self, val: int) -> int:
if len(self.pool) < self.k:
heapq.heappush(self.pool, val)
elif val > self.pool[0]:
heapq.heapreplace(self.pool, val)
return self.pool[0]
# Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)
239. 滑动窗口最大值 Sliding Window Maximum
第一种方法:用优先队列:大顶堆
第二种方法:因为窗口大小固定,只需要一个双端队列即可
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
if not nums:
return []
window, res = [], []
for i, x in enumerate(nums):
if i >= k and window[0] <= i - k:
window.pop(0)
while window and nums[window[-1]] <= x:
window.pop()
window.append(i)
if i >= k - 1:
res.append(nums[window[0]])
return res