用两个栈来实现一个队列,使用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
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
int result = -1;
if (!stack2.empty()){
result = stack2.top();
stack2.pop();
return result;
}
while (!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
if (!stack2.empty()){
result = stack2.top();
stack2.pop();
}
return result;
}
private:
stack<int> stack1;
stack<int> stack2;
};
这是左程云的《程序员代码面试指南》的答案:
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack1.empty()&&stack2.empty()){
throw new RuntimeException("Queue is empty!");
}
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
let stack1 = [] // 模拟队列头
let stack2 = [] // 模拟队列尾
function push(node)
{
// 向队尾插入元素
stack2.push(node)
}
function pop()
{
// 从队列头删除元素
if(stack1.length) {
return stack1.pop()
}
while(stack2.length) {
stack1.push(stack2.pop())
}
return stack1.pop()
}
module.exports = {
push : push,
pop : pop
}; public class NC76_stackToQueue {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if (!stack2.isEmpty()) return stack2.pop();
while (!stack1.isEmpty()) stack2.push(stack1.pop());
return stack2.pop();
}
} import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.peek());
stack1.pop();
}
}
int result = stack2.peek();
stack2.pop();
return result;
}
} import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if (!stack2.isEmpty()) {
return stack2.pop();
}
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.isEmpty() ? -1 : stack2.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): if len(self.stack2) !=0: return self.stack2.pop() while len(self.stack1) != 0: self.stack2.append(self.stack1.pop()) return self.stack2.pop()
let inStack = []; // 入队列
let outStack = []; // 出队列
function push(node)
{
inStack.push(node);
}
function pop()
{
// 如果outStack为空,就将inStack中的数据转移到outStack中
// 如果outStack不为空,就直接pop()
if(!outStack.length) {
while(inStack.length) {
outStack.push(inStack.pop());
}
}
return outStack.pop();
}
module.exports = {
push : push,
pop : pop
}; PS:不明白的同学,建议在纸上画一下,就懂了~# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stack = [] def push(self, node): # write code here self.stack.append(node) def pop(self): # return xx return self.stack.pop(0)
// push方法向栈中添加元素,返回结果是当前添加的元素
// pop方法移除并返回栈顶元素,如果是空栈,会抛出异常:EmptyStackException对队列 的push与对栈一样,向栈中添加元素
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while (!stack2.empty()){
stack1.push(stack2.pop());
}
stack1.push(node);
}
public int pop() {
while(!stack1.empty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
} import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
if(stack2.isEmpty())//如果栈2为空,则可直接入栈1,因为栈2的元素出栈顺序一定在栈1前面
stack1.push(node);
else{
while(!stack2.isEmpty()){//如果栈2非空,先将栈2的元素出栈加入到栈1,这样可以保持先加入元素的顺序一定在后加入元素顺序的前面
stack1.push(stack2.pop());
}
stack1.push(node);
}
}
public int pop(){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());//出栈所有栈1元素,加入到栈2,保持先进先出
}
return stack2.pop();
}
} import java.util.Stack;
public class Solution {
//stack1 作为入队栈
Stack<Integer> stack1 = new Stack<Integer>();
//stack2 作为出队栈
Stack<Integer> stack2 = new Stack<Integer>();
//LIFO 栈是后进先出
//FIFO 队列是先进先出
public void push(int node) {
//入队栈直接入栈
stack1.push(node);
}
public int pop() {
//出队栈为空时才到入队栈去拿元素,此时入栈元素位置颠倒刚刚与队列顺序一致
if(stack2.isEmpty()){
while (!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
//利用 stack1 进行入队列存储
public void push(int node) {
stack1.push(node);
}
//利用 stack2 进行出队列存储
//如果 stack2 不为空,则直接进行出队列操作
//如果 stack2 为空,则先将stack1的数据存储到 stack2 ,再进行出队列操作
public int pop() {
if(stack2.empty()) {
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
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中。