LeetCode#225-Implement Stack using Queues-用队列实现栈

一、题目

使用队列实现栈的下列操作:

  • push(x) -- 元素 x 入栈
  • pop() -- 移除栈顶元素
  • top() -- 获取栈顶元素
  • empty() -- 返回栈是否为空

注意:

  • 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

题解

我们都知道,栈是“先进后出”的数据结构,栈内元素从顶端压入(push),从顶端弹出(pop)。队列与栈相反,是“先进先出”的数据结构,队列中元素只能从后端(rear)入队(push),然后从前端(front)端出队(pop)。

  • 解法1:两个队列

这里用的是数组表示队列,空间复杂度O(n),时间复杂度分push 和 pop ,前者是O(1),后者是O(n)。

class MyStack {
    /**
     * Initialize your data structure here.
     */
    function __construct() {
        $this->q1 = [];
        $this->q2 = [];
    }

    /**
     * Push element x onto stack.
     * @param Integer $x
     * @return NULL
     */
    function push($x) {
        $this->q1[] = $x;
    }

    /**
     * Removes the element on top of the stack and returns that element.
     * @return Integer
     */
    function pop() {
        while(count($this->q1) > 1) {
            $this->q2[] = array_shift($this->q1);
        }
        $top = array_shift($this->q1);
        $this->q1 = $this->q2;
        $this->q2 = [];
        return $top;
    }

    /**
     * Get the top element.
     * @return Integer
     */
    function top() {
        return end($this->q1);
    }

    /**
     * Returns whether the stack is empty.
     * @return Boolean
     */
    function empty() {
        return empty($this->q1) ? true : false;
    }
}
  • 解法2:单队列

用一个队列,将新元素压入队尾,然后将队首元素弹出,放到新元素后面,直到新元素在队首为止,此时队首元素就是栈顶元素。

空间复杂度O(n),时间复杂度分push 和 pop ,前者是O(n),后者是O(1)。

class MyStack2 {
    /**
     * Initialize your data structure here.
     */
    function __construct() {
        $this->q = [];
    }

    /**
     * Push element x onto stack.
     * @param Integer $x
     * @return NULL
     */
    function push($x) {
        $this->q[] = $x;
        $len = count($this->q);
        while ($len > 1) {
            $this->q[] = array_shift($this->q);
            $len--;
        }
    }

    /**
     * Removes the element on top of the stack and returns that element.
     * @return Integer
     */
    function pop() {
        return array_shift($this->q);
    }

    /**
     * Get the top element.
     * @return Integer
     */
    function top() {
        return $this->q[0];
    }

    /**
     * Returns whether the stack is empty.
     * @return Boolean
     */
    function empty() {
        return empty($this->q) ? true : false;
    }
}

原文地址:https://www.cnblogs.com/sunshineliulu/p/12547737.html

时间: 2024-08-05 03:36:20

LeetCode#225-Implement Stack using Queues-用队列实现栈的相关文章

leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java

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 s

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

题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个 ps:用栈实现队列,参考剑指offer 1 class Stack { 2 private: 3 queue<int> q[2]; 4 int flag=0; 5 public: 6 // Push element x onto stack. 7 void push(int

leetcode_225题——Implement Stack using Queues (队列,栈)

Implement Stack using Queues Total Accepted: 9609 Total Submissions: 31710My Submissions Question Solution Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the st

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

翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列. 也就是说,仅仅有push/pop/size/empty等操作是有效的. 队列可能不被原生支持.这取决于你所用的语言. 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列. 你能够如果全部的操作都是有效的(

[leetcode] 225. Implement Stack using Queues

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 s

(easy)LeetCode 225.Implement Stack using Queues

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 s

[LeetCode] 225. Implement Stack using Queues Java

题目: 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 on

Java for LeetCode 225 Implement Stack using Queues

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 s

Implement Stack using Queues 用队列实现栈

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. 解题思路: 用队列实现栈的操作.包括输入.输出.取栈

Leetcode 225 Implement Stack using Queues STL

用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的队列中取出front() 1 class Stack { 2 public: 3 queue<int> q[2]; 4 5 // Push element x onto stack. 6 void move(int x){ 7 while(!q[1-x].empty()){ 8 q[x].push