225. Implement Stack using Queues

實作 stack

class MyStack {
    private $stack = [];
    /**
     */
    function __construct() {
        
    }
  
    /**
     * @param Integer $x
     * @return NULL
     */
    function push($x) {
        $this->stack[] = $x;
    }
  
    /**
     * @return Integer
     */
    function pop() {
        return array_pop($this->stack);
    }
  
    /**
     * @return Integer
     */
    function top() {
        return $this->stack[array_key_last($this->stack)];
    }
  
    /**
     * @return Boolean
     */
    function empty() {
        return empty($this->stack);
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * $obj = MyStack();
 * $obj->push($x);
 * $ret_2 = $obj->pop();
 * $ret_3 = $obj->top();
 * $ret_4 = $obj->empty();
 */

Last updated