用两个栈来实现一个队列,使用n个元素来完成 n 次在队列尾部插入整数(push)和n次在队列头部删除整数(pop)的功能。 队列中的元素为int类型。保证操作合法,即保证pop操作时队列内已有元素。
数据范围:
要求:存储n个元素的空间复杂度为 ,插入与删除的时间复杂度都是
["PSH1","PSH2","POP","POP"]
1,2
"PSH1":代表将1插入队列尾部 "PSH2":代表将2插入队列尾部 "POP“:代表删除一个元素,先进先出=>返回1 "POP“:代表删除一个元素,先进先出=>返回2
["PSH2","POP","PSH1","POP"]
2,1
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stackA=[] self.stackB=[] def push(self, node): self.stackA.append(node) # write code here def pop(self): if not self.stackB: while self.stackA: self.stackB.append(self.stackA.pop()) return self.stackB.pop() # return xx
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stack1 = [] # 入栈 self.stack2 = [] # 出栈 def push(self, node): # write code here self.stack1.append(node) def pop(self): # return xx # 如果出栈的没有值,先把入栈的数据全部拿过去 if not self.stack2: while self.stack1: self.stack2.append(self.stack1.pop()) # 出栈的数据弹出 return self.stack2.pop()
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.a = [] self.b = [] def push(self, node): self.a.append(node) def pop(self): if len(self.b) > 0: return self.b.pop() while(len(self.a) > 0): self.b.append(self.a.pop()) return self.b.pop()
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stack1 = [] # 入栈 self.stack2 = [] # 出栈 def push(self, node): # write code here self.stack1.append(node) # 入值 def pop(self): # return xx if self.stack2 == []: # 将入栈存入B栈 while self.stack1: self.stack2.append(self.stack1.pop()) return self.stack2.pop()
解题思路: 1、初始化两个栈,list实现; 2、push操作直接往栈1append元素; 3、pop操作分两种情况:1、如果栈2不空,则栈2出栈;2、如果栈2为空,则将栈1所有元素出栈,然后写入栈2后,再将栈2元素出栈。class Solution: def __init__(self): self.stack1 = [] self.stack2 = [] def push(self, node): # write code here self.stack1.append(node) def pop(self): # return xx if self.stack2: return self.stack2.pop() elif not self.stack1: return None else: while self.stack1: self.stack2.append(self.stack1.pop()) return self.stack2.pop()
class Solution: def __init__(self): self.zhan1 = [] self.zhan2 = [] def push(self, node): self.zhan2_to_zhan1() self.zhan1.append(node) self.zhan1_to_zhan2() def pop(self): self.zhan1_to_zhan2() r = self.zhan2[0] del self.zhan2[0] self.zhan2_to_zhan1() return r def zhan1_to_zhan2(self): self.zhan2.clear() for i in self.zhan1: self.zhan2.append(i) def zhan2_to_zhan1(self): self.zhan1.clear() for i in self.zhan2: self.zhan1.append(i)
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.res_list = [] def push(self, node): # write code here self.res_list.append(node) def pop(self): # return xx if self.res_list: res = self.res_list.pop(0) return res
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.res_list1 = [] self.res_list2 = [] def push(self, node): # write code here self.res_list1.append(node) def pop(self): # return xx if self.res_list2: res = self.res_list2.pop() return res if not self.res_list1: return if self.res_list1: while self.res_list1: self.res_list2.append(self.res_list1.pop()) res = self.res_list2.pop() return res
class Solution: def __init__(self): self.s1 = [] self.s2 = [] def push(self, node): self.s1.append(node) def pop(self): if self.s2 == []: while self.s1 : a = self.s1.pop() self.s2.append(a) return self.s2.pop()
if self.s2==[]此句判断栈2是否为空
return self.s2.pop()是s2不为空则执行的语句。
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stackA=[] self.stackB=[] def push(self, node): self.stackA.append(node) def pop(self): if self.stackB: tmp = self.stackB[-1] del self.stackB[-1] return tmp elif not self.stackA: return 'None' else: # 把栈A中的全部放入B中,再取其中一个 while self.stackA: self.stackB.append(self.stackA[-1]) del self.stackA[-1] return self.pop()
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.s1 = list() def push(self, node): # write code here self.s1.append(node) def pop(self): # return xx aa = self.s1[0] del self.s1[0] return aa
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stackA = [] self.stackB = [] def push(self, node): (1489)# write code here while self.stackB != []: self.stackA.append(self.stackB.pop()) return self.stackA.append(node) def pop(self): # return xx while self.stackA != []: self.stackB.append(self.stackA.pop()) return self.stackB.pop()
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.sa=[] self.sb=[] def push(self, node): (1413)# write code here self.sa.append(node) def pop(self): # return xx if len(self.sa)==0: return None while len(self.sa)!=1: self.sb.append(self.sa.pop()) renum=self.sa.pop() if len(self.sb)>0: self.sa.append(self.sb.pop()) return renum
class Solution { public: void push(int node) { stack1.push(node); }
int pop() { int a; if(stack2.empty()){ while(!stack1.empty()){ a=stack1.top(); stack2.push(a); stack1.pop(); } } a=stack2.top(); stack2.pop(); return a; }
private: stack<int> stack1; stack<int> stack2; };
用两个栈实现一个队列的功能?要求给出算法和思路!
<分析>:
入队:将元素进栈A
出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;
如果不为空,栈B直接出栈。
用两个队列实现一个栈的功能?要求给出算法和思路!
<分析>:
入栈:将元素进队列A
出栈:判断队列A中元素的个数是否为1,如果等于1,则出队列,否则将队列A中的元素 以此出队列并放入队列B,直到队列A中的元素留下一个,然后队列A出队列,再把 队列B中的元素出队列以此放入队列A中。