用两个栈来实现一个队列,使用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
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.pop());
}
}
if(stack2.isEmpty()) return 0;
return stack2.pop();
}
}
import java.util.*;
import java.util.Stack;
public class Solution {
// 用于push的栈
Stack<Integer> stack1 = new Stack<Integer>();
// 用于pop的栈
Stack<Integer> stack2 = new Stack<Integer>();
// 进队
public void push(int node) {
stack1.add(node);
}
// 出队(重点)
public int pop() {
if (!stack2.isEmpty()) {
// 若pop栈非空,则直接弹出pop栈
return stack2.pop();
}
// 若pop栈空,则把push栈的内容依次转移到pop栈,再弹出
while (!stack1.isEmpty()) {
int temp = stack1.pop();
stack2.push(temp);
}
return stack2.pop();
}
}
import java.util.*;
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.pop());
}
}
return stack2.pop();
}
}
import java.util.*;
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() {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
int result = stack2.pop();
while(!stack2.isEmpty()){
stack1.push(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() {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
};
return stack2.pop();
}
}
// 当向队列中插入数据时,实际上将其插入栈stack1中
// 当要从队列中弹出队首元素时,就可以将stack1中的元素依次写入到stack2中(由于在stack1中元素的顺序与队列中的正好相反,所以再次反即为正,反反得正)
// 所以这个时候,从栈stack2中弹出的栈顶元素即为队列中的队首元素 import java.util.*;
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();
}
//栈1的数据全部进入栈2
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
//进栈之后在立即出一个栈
return stack2.pop();
}
public static void main(String[] args) {
Solution stackDemo = new Solution();
for (int i = 0; i < 10; i++) {
stackDemo.push(i);
//栈 先进后出 stack1是 9 8 7 。。。。 1
}
System.out.println("1111111");
if (stackDemo.stack2.isEmpty()) {
int pop = stackDemo.pop();
System.out.println(pop);
}
while (!stackDemo.stack2.isEmpty()) {
int pop = stackDemo.pop();
System.out.println(pop);
}
}
}
public void push(int node) {//从栈1入
stack1.push(node);
}
public int pop() {
if(stack2.size()<=0){//从栈2出
while(stack1.size()!=0){//当栈1不为0
stack2.push(stack1.pop());
}
}
return stack2.pop();
} import java.util.*;
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() {
while(stack1.size() > 1) {
stack2.push(stack1.pop());
}
int val = stack1.pop();
while(!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return val;
}
}
import java.util.*;
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.add(node);
}
public int pop() {
int item = stack1.firstElement();
stack1.removeElementAt(0);
return item;
}
} 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.isEmpty()){
stack1.add(stack2.pop());
}
stack1.add(node);
}
public int pop() {
while(!stack1.isEmpty()){
stack2.add(stack1.pop());
}
return stack2.pop();
}
}
public class Solution {
// 用于入队列
Stack<Integer> stack1 = new Stack<Integer>();
// 专用于出队列
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.add(node);
}
public int pop() {
if(!stack2.isEmpty()){
return stack2.pop();
} else {
while (!stack1.isEmpty()){
stack2.add(stack1.pop());
}
return this.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中。