剑指offer刷题日记-栈 队列 堆 哈希表

栈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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值