Leetcode基础刷题之PHP解析(225. Implement Stack using Queues)

2019-4-9   

 Leetcode之PHP版题目解析(9. Palindrome Number)

4ab334814322f7d53168cd257cba0c90.png

使

使PHParray_shift()array_unshift().作。


1

 private $queue1=[];
     private $queue2=[];
  
    /**
     * Push element x onto stack.
     * @param Integer $x
     * @return NULL
     */
    function push($x) {
        array_push($this->queue2,$x);
        while(!empty($this->queue1)){
            array_push($this->queue2,array_shift($this->queue1));
        }
        $temp=$this->queue1;
        $this->queue1=$this->queue2;
        $this->queue2=$temp;
    }
  
    /**
     * Removes the element on top of the stack and returns that element.
     * @return Integer
     */
    function pop() {
        return array_shift($this->queue1);
    }
  
    /**
     * Get the top element.
     * @return Integer
     */
    function top() {
        return current($this->queue1);
    }
  
    /**
     * Returns whether the stack is empty.
     * @return Boolean
     */
    function empty() {
        return empty($this->queue1)==true;
    }

Github整理地址:https://github.com/wuqinqiang/leetcode-php

吴亲库里 has written 100 articles

情绪只是对自己无能的愤怒

积分:4877 等级:P8 职业:php 城市:杭州

0 条回复

登录后才能进行评论,立即登录?