标签:public 功能 输出 value queue 思路 vat empty values
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead?操作返回 -1 )
示例:
输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出:[null,null,3,-1]
示例2:
输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]
提示:
class CQueue {
public:
CQueue() {
}
void appendTail(int value) {
int val;
while(!st2.empty()){
val = st2.top();
st2.pop();
st1.push(val);
}
st1.push(value);
}
int deleteHead() {
if(st1.empty() && st2.empty()){
return -1;
}
int val;
while(!st1.empty()){
val = st1.top();
st1.pop();
st2.push(val);
}
val = st2.top();
st2.pop();
return val;
}
private:
stack<int> st1;
stack<int> st2;
};
*/
进A栈,出A栈,逆序,进B栈,出B栈,正序,则A栈负责进,B栈负责出
[leetcode]剑指 Offer 09. 用两个栈实现队列
标签:public 功能 输出 value queue 思路 vat empty values
原文地址:https://www.cnblogs.com/Swetchine/p/13221646.html