来自 澳门金莎娱乐手机版 2019-11-14 16:49 的文章
当前位置: 金沙澳门官网网址 > 澳门金莎娱乐手机版 > 正文

用队列来达成栈,用栈来达成队列

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

翻译

用队列来实现栈的如下操作。

push(x) —— 将元素x添加进栈
pop() —— 从栈顶移除元素
top() —— 返回栈顶元素
empty() —— 返回栈是否为空

注意:

你必须使用一个只有标准操作的队列。

也就是说,只有push/pop/size/empty等操作是有效的。

队列可能不被原生支持,这取决于你所用的语言。

只要你只是用queue的标准操作,你可以用list或者deque(double-ended queue)来模拟队列。

你可以假设所有的操作都是有效的(例如,pop或peek操作不会被用在空栈上)。

澳门金莎娱乐手机版 ,翻译

用栈来实现队列的下列操作。

push(x) —— 将元素x写入到队列的尾部
pop() —— 从队列首部移除元素
peek() —— 返回队列首部元素
empty() —— 返回队列是否为空

注意:

你必须使用一个只有标准操作的栈。

也就是说,只有push/pop/size/empty等操作是有效的。

栈可能不被原生支持,这取决于你所用的语言。

只要你只是用stack的标准操作,你可以用list或者deque(double-ended queue)来模拟栈。

你可以假设所有的操作都是有效的(例如,pop或peek操作不会被用在空队列上)。

原文

Implement the following operations of a stack using queues.

push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.

Notes:

You must use only standard operations of a queue 

-- which means only push to back, peek/pop from front, size, and is empty operations are valid.


Depending on your language, queue may not be supported natively. 

You may simulate a queue by using a list or deque (double-ended queue), 

as long as you use only standard operations of a queue.


You may assume that all operations are valid (for example, 

no pop or top operations will be called on an empty stack).

原文

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 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).

本文由金沙澳门官网网址发布于澳门金莎娱乐手机版,转载请注明出处:用队列来达成栈,用栈来达成队列

关键词: