剑指offer-两个栈模拟队列

来源:互联网 发布:tp框架源码 编辑:程序博客网 时间:2024/06/06 09:49

java的逻辑很简单:

  • 两个栈模拟队列:
               
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());    }    }    if(stack2.isEmpty()){    return 0;    }    return stack2.pop();    }}


0 0
原创粉丝点击