标签:eth cpp ++ queue public top 元素 returns ini
使用队列实现栈的下列操作:
+你只能使用队列的基本操作-- 也就是?push to back
, peek/pop from front
, size
, 和?is empty
?这些操作是合法的。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push(x);
for (int i = 0; i + 1 < que.size(); i++) {
que.push(que.front());
que.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int val = top();
que.pop();
return val;
}
/** Get the top element. */
int top() {
return que.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
private:
queue<int> que;
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
飘了飘了~~~
标签:eth cpp ++ queue public top 元素 returns ini
原文地址:https://www.cnblogs.com/coding365/p/12652548.html