用两个栈来实现一个队列,使用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
function push(node)
{
while(stack2.length > 0) {
let oldNode = stack2.pop()
stack1.push(oldNode)
}
stack1.push(node)
}
function pop()
{
while(stack1.length > 0) {
let node = stack1.pop()
stack2.push(node)
}
return stack2.pop()
}
let stack1 = [], stack2 = []
module.exports = {
push : push,
pop : pop
}; var stack1 = [] // 入栈stack1----负责存元素
var stack2 = [] // 出栈stack2----负责取元素
function push(node)
{
// write code here
// 入栈时直接stack1存入元素
stack1.push(node)
}
function pop()
{
// write code here
// 如果stack2中没有元素,则从stack1中将元素pop出,再push存入stack2中
if(stack2.length == 0) {
while(stack1.length) {
stack2.push(stack1.pop())
}
}
return stack2.pop() // 将stack2元素弹出
}
module.exports = {
push : push,
pop : pop
};
const stackIn = []
const stackOut = []
function push(node) {
// write code here
stackIn.push(node)
}
function pop() {
// write code here
// 有的话证明上次队列没有完全出队
if(stackOut.length) return stackOut.pop()
// 到这里说明已经完全出队了, 需要进入下批次的队伍
while (stackIn.length) {
stackOut.push(stackIn.pop())
}
return stackOut.pop()
}
module.exports = {
push: push,
pop: pop
};
var stack1 = [];
var stack2 = [];
function push(node)
{
// write code here
stack1.push(node);
}
function pop()
{
// write code here
if(stack2.length == 0){
while(stack1.length != 0){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
module.exports = {
push : push,
pop : pop
}; let stack1 = [],stack2 = [];
function push(node)
{
// write code here
stack1.push(node);
}
function pop()
{
let tmp;
while(tmp = stack1.pop()){
stack2.push(tmp);
}
let res = stack2.pop();
while(tmp = stack2.pop()){
stack1.push(tmp);
}
return res;
}
队列的pop是pop第一个元素,栈的pop是pop最后一个元素,数组的pop与栈一样
const stack = [];
function push(node)
{
// write code here
stack.push(node);
}
function pop()
{
// write code here
return stack.shift();
}
module.exports = {
push : push,
pop : 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:不明白的同学,建议在纸上画一下,就懂了~let stack1 = [],stack2 = []
function push(node)
{
// write code here
stack1.push(node)// 当时进队列,直接进入 stack1
}
function pop()
{
// write code here
if(stack2.length) return stack2.pop() //当出列时,如果 stack2 不为空,弹出 stack2 栈顶元素
else{
while(stack1.length){
stack2.push(stack1.pop()) // stack1 中的全部数逐个出栈入栈 stack2
}
}
return stack2.pop() //弹出 stack2 栈顶元素
} /**
* 我的解题思路:
* 先入后出的话,直接调用shift方法就好了
*
* @param {*} node
*/
const stack = [];
function push(node)
{
// write code here
stack.push(node);
}
function pop()
{
// write code here
return stack.shift();
}
/**
* 不用其他的方法的解题思路:
* 1.主要是pop方法的实现
* 2.先生成一个倒序的栈,用于获取需要返回的结果
* 3.然后再将之前栈填充完整
*
* @param {*} node
*/
let stack1 = [];
let stack2 = [];
function topPush(node)
{
// write code here
stack1.push(node);
}
function topPop()
{
// write code here
while(stack1.length) {
stack2.push(stack1.pop());
}
const result = stack2.pop();
while(stack2.length) {
stack1.push(stack2.pop());
}
return result;
}
var inStack = [],
outStack = [];
function push(node)
{
// write code here
inStack.push(node);
}
function pop()
{
// write code here
if (!outStack.length) {
while (inStack.length) {
outStack.push(inStack.pop());
}
}
return outStack.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中。