栈05-两个栈实现队列(python)
#stack1入 stack2出
class Solution:
def __init__(self):
self.stack1=[]
self.stack2=[]
def push(self,node):
self.stack1.append(node)
def pop(self):
if len(self.stack1)==0 or len(self.stack2)==0:
return
elif len(self.stack2)==0:
while len(self.stack1)>0:
self.stack2.append(self.stack1.pop())
栈20-包含min函数的栈(python)
#辅助栈minstack
class Solution:
def __init__(self):
self.stack=[]
self.minstack=[]
def push(self,node):
if not self.stack:
self.stack.append(node)
else:
if self.minstack[-1]<node:
self.minstack.append(self.minstack[-1])
else:
self.minstack.append(node)
def pop(self):
self.stack.pop(-1)
self.minstack.pop(-1)
def top(self):
self.stack.pop(-1)
def min(self):
self.minstack.pop(-1)
栈21-判断栈的压入弹出(python)
#stack1入 stack2出
class Solution:
def IsPopOrder(self,pushV,popV):
stack=[]
for i in pushV:
stack.append(i)
while stack and stack[-1]==popV[0]:
stack.pop()
popV.pop(0)
if len(stack)==0:
return True
else:
return False
栈044-反转单词顺序列(python)
#按空格拆分
class Solution:
def reverseSetence(self,s):
stack=[n for n in s.split(' ')]
stack.reverse()
return ''.join(stack)
队列064-滑动窗口的最大值(python)
class Solution:
def maxWindows(self,num,size):
if not nums or size<=0:
return []
queue=[]
res=[]
for i in range(len(num)):
while queue and queue[0]<=i-size:
queue.pop(0)
while queue and nums[i]>num[queue[-1]]:
queue.pop(-1)
queue.append(i)
if i>=size-1:
res.append(num[queue[0])
return res
堆29-最小的k个数(python)
class Solution:
def GetLeastNumbers_solution(self,tinput,k):
if not tinput or k>len(tinput):
return []
tinput=self.quicksort(tinput)
return tinput[:k]
def quicksort(self,list):
if not list:
return []
pivot=list[0]
left=self.quicksort([x for x in list[1:] if x<pivot)
right=self.quicksort([x for x in list[1:] if x>=pivot)
return left+[pivot]+right
哈希表034-第一个只出现一次的字符(python)
class Solution:
def FistNotRepeating(self,s):
dict={}
for i in s:
dicts[i]=dicts.get(i,0)+1
for i in s:
if dicts[i]==1:
return i
return -1