LeetCode232——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.

Notes:

  • You must use only standard operations of a stack -- which means only push
    to top
    peek/pop from topsize,
    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).

实现:

class Queue {

public:

// Push element x to the back of queue.

void push(int x) {

deq.push_back(x);

if (deq.size() == 1) {

front = x;

}

}

// Removes the element from in front of queue.

void pop(void) {

vector<int> vec;

while (deq.size() > 0) {

vec.push_back(deq.back());

deq.pop_back();

}

if (vec.size() > 0) {

for (int i=vec.size()-1-1; i >= 0; i--) {

push(vec[i]);

}

}

}

// Get the front element.

int peek(void) {

return front;

}

// Return whether the queue is empty.

bool empty(void) {

return deq.size() == 0;

}

private:

deque<int> deq;

int front;

};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 13:20:35

LeetCode232——Implement Queue using Stacks的相关文章

LeetCode232: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. Notes: You mus

LeetCode232 Implement Queue using Stacks Java 题解

题目: 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. Notes:

LeetCode-232 Implement Queue using Stacks Solution (with Java)

1. Description: Notes:  2. Examples: 3.Solutions: 1 /** 2 * Created by sheepcore on 2019-03-07 3 * Your MyQueue object will be instantiated and called as such: 4 * MyQueue obj = new MyQueue(); 5 * obj.push(x); 6 * int param_2 = obj.pop(); 7 * int par

【LeetCode】232. Implement Queue using Stacks

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 t

leetcode_232——Implement Queue using Stacks(栈与队列)

Implement Queue using Stacks Total Accepted: 5687 Total Submissions: 16629My Submissions Question Solution Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from i

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

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 whet

[LeetCode][JavaScript]Implement Queue using Stacks

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 t

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

Lintcode: Implement Queue by Stacks 解题报告

Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is