来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.Stack;
class CQueue {
private Stack<Integer> s1;
private Stack<Integer> s2;
public CQueue() {
this.s1 = new Stack<>();
this.s2 = new Stack<>();
}
public void appendTail(int value) {
s1.push(value);
}
public int deleteHead() {
if (s2.isEmpty()) {
while (! s1.isEmpty()) {
s2.push(s1.pop());
}
if (s2.isEmpty()) {
return -1;
}
}
return s2.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
原文:https://blog.51cto.com/tianyiya/2530932