首页 > 其他分享 >LeetCode: 232. Implement Queue using Stacks

LeetCode: 232. Implement Queue using Stacks

时间:2022-12-05 18:05:16浏览次数:41  
标签:queue pop Queue MyQueue que push using Stacks empty


LeetCode: 232. Implement Queue using Stacks

题目描述

Implement the following operations of a queue using stacks.

​push(x)​​​ – Push element x to the back of queue.
​​​pop()​​​ – Removes the element from in front of queue.
​​​peek()​​​ – Get the front element.
​​​empty()​​​ – Return whether the queue is empty.
Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false

Notes:

  • You must use only standard operations of a stack – which means only ​​push​​​ to top, ​​peek​​​/​​pop​​​ from top, ​​size​​​, and is ​​empty​​ operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

解题思路

用两个栈来模拟队列。

AC 代码

class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {

}

/** Push element x to the back of queue. */
void push(int x) {
m_stk.push(x);
}

/** Removes the element from in front of queue and returns that element. */
int pop() {
if(m_que.empty())
{
MoveStk2Que();
}

int front = m_que.top();
m_que.pop();
return front;
}

/** Get the front element. */
int peek() {
if(m_que.empty())
{
MoveStk2Que();
}

return m_que.top();
}

/** Returns whether the queue is empty. */
bool empty() {
return (m_stk.empty() && m_que.empty());
}
private:
void MoveStk2Que()
{
while(!m_stk.empty())
{
m_que.push(m_stk.top());
m_stk.pop();
}
}
private:
stack<int> m_stk;
stack<int> m_que;
};

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/


标签:queue,pop,Queue,MyQueue,que,push,using,Stacks,empty
From: https://blog.51cto.com/u_15903085/5913203

相关文章

  • ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Y
    在Ubuntu下想要登录mysql数据库root@JD:~#mysql-uroot-p报错ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:YES)导致登录......
  • java 中的Stack、Queue、Deque
    1.Stackjava集合框架中没有Stack接口,仅仅有java早期遗留下来的一个Stack类。Dequestack=newArrayDeque();publicStackextendsVector因为集成自Vector,所以Stack类是......
  • ConConcurrentQueue Testing
    //ProvidesaC++11implementationofamulti-producer,multi-consumerlock-freequeue.//Anoverview,includingbenchmarkresults,isprovidedhere://http......
  • DelayQueue的简单介绍
    DelayQueue按照延迟时间从小到大出队列的队列,延迟时间表示的是未来将要执行的时间减去当前的时间,对于加入DelayQueue的元素,需要实现Delayed接口  当getDelay()的返回......
  • ORACLE 参数 job_queue_processes
    官方文档中的说明:JOB_QUEUE_PROCESSES specifiesthemaximumnumberofjobslavesperinstancethatcanbecreatedfortheexecutionof DBMS_JOB jobsandOrac......
  • using关键字在C#中的用法
    using关键字有两个主要用途: (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型。 (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象。(一).......
  • Rabbitmq #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue ‘
    #method<channel.close>(reply-code=404,reply-text=NOT_FOUND-noqueue‘myQueueDirect‘invhost‘/‘是因为消息发布者queueDeclare方法中传递的exclusive参数传......
  • SynchronousQueue的简单介绍
    SynchronousQueue有点特殊,具备生产者和消费者,但是生产者生产后会执行阻塞,当产生的数据有人取走的时候,生产者的阻塞状态会解除,再次生产再次阻塞,再次等待消费者解锁,多个线程......
  • 打印队列(Printer Queue)
    PrinterQueueTimelimit:3.000seconds【分析】      首先记录所求时间它在队列中的位置,用一个队列存储这些任务的优先级,同时也创建一个队列存储对应任务一开始的......
  • Try creating the file referenced by the URI, or Try using a URI for a file that
     flutter问题如下:在添加组件的时候报错,pubspec.yaml中的组件正常加载,但是在页面引入组件会报错  Type:StringTargetofURIdoesn'texist:'package:getwidge......