LeetCode 232. 用栈实现队列
题目:https://leetcode.cn/problems/implement-queue-using-stacks/
解析:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html
python中栈的初始化是:stack = []
,直接用数组表示。
题目里写:
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
对应python中栈的使用就是append, pop, len, not stack_name
更具体来说,针对peek值,不可以直接访问stack[0]
的元素
我们的做法是把两个栈拼起来:
class MyQueue(object):
def __init__(self):
self.stack_in = []
self.stack_out = []
def push(self, x):
"""
:type x: int
:rtype: None
"""
self.stack_in.append(x)
def pop(self):
"""
:rtype: int
"""
if self.empty():
return None
if self.stack_out:
return self.stack_out.pop()
else:
while self.stack_in:
self.stack_out.append(self.stack_in.pop())
return self.stack_out.pop()
def peek(self):
"""
:rtype: int
"""
peek = self.pop()
self.stack_out.append(peek)
return peek
def empty(self):
"""
:rtype: bool
"""
return not self.stack_in and not self.stack_out
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
LeetCode 225. 用队列实现栈
题目:https://leetcode.cn/problems/implement-stack-using-queues/
解析:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html
python中栈的初始化是:
from collections import deque
queue = deque()
题目里写:
你只能使用队列的标准操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
对应python中栈的使用就是append, popleft, len, not queue_name
与上题一致,针对top值,不可以直接访问queue[0]
的元素
我们的做法是不断循环队列:
class MyStack:
def __init__(self):
self.queue_in = deque()
def push(self, x: int) -> None:
self.queue_in.append(x)
def pop(self) -> int:
if self.empty():
return None
for _ in range(len(self.queue_in) - 1):
self.queue_in.append(self.queue_in.popleft())
return self.queue_in.popleft()
def top(self) -> int:
if self.empty():
return None
for _ in range(len(self.queue_in) - 1):
self.queue_in.append(self.queue_in.popleft())
res = self.queue_in.popleft()
self.queue_in.append(res)
return res
def empty(self) -> bool:
return not self.queue_in
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
LeetCode 20. 有效的括号
题目:https://leetcode.cn/problems/valid-parentheses/
解析:https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html
把正括号压入栈中,如果遇到反括号,判断栈顶元素是否为对应的正括号。
class Solution:
def isValid(self, s: str) -> bool:
if len(s) % 2 != 0:
return False
stack = []
dic = {
')': '(',
']': '[',
'}': '{'
}
for c in s:
if c not in dic:
stack.append(c)
else:
if not stack or not stack.pop() == dic[c]:
return False
if stack:
return False
else:
return True
LeetCode 1047. 删除字符串中的所有相邻重复项
题目:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
解析:https://programmercarl.com/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.html
把字符串依次压入栈,遇到相同字母,则消除
class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for c in s:
if stack and stack[-1] == c:
stack.pop()
else:
stack.append(c)
return ''.join(stack)