2. 使用队列实现栈 (LeetCode 225 Implement Stack using Queues)
2.1题目
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
classMyStack(object):def__init__(self):"""
Initialize your data structure here.
"""
self.stack = collections.deque([])
defpush(self, x):"""
self.stack = []
Push element x onto stack.
:type x: int
:rtype: void
"""
self.stack.append(x)
q = self.stack
for i in range(len(q) - 1):
q.append(q.popleft())
defpop(self):"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""return self.stack.popleft()
deftop(self):"""
Get the top element.
:rtype: int
"""return self.stack[0]
defempty(self):"""
Returns whether the stack is empty.
:rtype: bool
"""if len(self.stack) == 0:
returnTrueelse:
returnFalse
3. 使用栈实现队列 (LeetCode 232)
3.1题目
Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
classMyQueue(object):def__init__(self):"""
Initialize your data structure here.
"""
self.input = []
self.output = []
defpush(self, x):"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.input.append(x)
defpop(self):"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
self.peek()
return self.output.pop()
defpeek(self):"""
Get the front element.
:rtype: int
"""if self.output == []:
while self.input:
self.output.append(self.input.pop())
return self.output[-1]
defempty(self):"""
Returns whether the queue is empty.
:rtype: bool
"""return self.input == [] and self.output == []
4. 包含min函数的栈(LeetCode 155 Min Stack)
4.1题目
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.
Example:
Class Solution:
defIsPopOrder(self, pushV, popV):if pushV == [] or popV == []:
returnFalse
stack = []
for i in pushV:
stack.append(i)
while len(stack) and stack[-1] == popV[0]:
stack.pop()
popV.pop(0)
if stack != []:
returnFalseelse:
returnTrue
7. 数组中的第K大的数 (LeetCode 215 Kth Largest Element in an Array)
7.1题目
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.**
7.2思路
思路1:先对数组排序,再求解第k个元素
思路2:使用最小堆做,第k大的数即len(q)-k小的元素
7.3代码
#排序法classSolution(object):deffindKthLargest(self, nums, k):"""
:type nums: List[int]
:type k: int
:rtype: int
"""
nums.sort()
return nums[-k]
#最小堆法classSolution(object):deffindKthLargest(self, nums, k):"""
:type nums: List[int]
:type k: int
:rtype: int
"""
heap = []
for num in nums:
heapq.heappush(heap, num)
for i in range(len(nums) - k):
heapq.heappop(heap)
return heapq.heappop(heap)
8. 寻找中位数(LeetCode 295 Find Median from Data Stream)
8.1题目
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:
[2,3,4] , the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
void addNum(int num) - Add a integer number from the data stream to the data structure.
double findMedian() - Return the median of all elements so far.