用php完成LeetCode – Evaluate Reverse Polish Notation

本文介绍了一个使用PHP实现的逆波兰表达式求值算法。通过自定义栈类来处理运算符和操作数,该算法能正确解析并计算逆波兰表达式的值。文章提供了一个具体的例子,演示了如何使用此算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以我写的前一篇的‘关于php中栈的基本操作’ http://blog.youkuaiyun.com/martinhacker/article/details/60781815 完成的leetcode 的一道栈的问题,phper可以参考一下。
题目:http://www.programcreek.com/2012/12/leetcode-evaluate-reverse-polish-notation/

<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 07/03/2017
 * Time: 21:35
 */

class node{
    private $value;
    private $pre;
    public function __construct($value){
        $this->value = $value;
        $this->pre = null;
    }
    public function addPre($node){
        $this->pre = $node;
    }
    public function getPre(){
        return $this->pre;
    }
    public function getValue(){
        return $this->value;
    }
}

class stack{
    private $top;
    static public $size;
    public function __construct($value = null){
        $this->top = new node($value);
    }

    public function push($value){
        $current = $this->top;
        $newNode = new node($value);
        $newNode->addPre($current);
        $this->top = $newNode;
    }

    public function getAllStack(){
        $stack = null;
        $current = $this->top;
        while ($current->getPre() != null){
            $stack .= $current->getValue()."\n";
            $current = $current->getPre();
        }
        return $stack;
    }

    public function getSize(){
        $current = $this->top;
        while (null != $current->getValue()){
            self::$size++;
            $current = $current->getPre();
        }
        return self::$size;
    }

    public function pop(){
        $tmp = $current = $this->top;
        $this->top = $current->getPre();
        unset($current);
        return $tmp->getValue();
    }

    public function getTop(){
        return $this->top->getValue();
    }

    public function isempty(){
        if (null == $this->getTop()){
            return false;
        }
        return true;
    }
}

class stackReverse{
    public function calculate($arrs){
        $stack = new stack();
        $operator = '+-*/';
        foreach ($arrs as $arr){
            if (!strstr($operator, $arr)){
                $stack->push($arr);
            }else{
                if ($stack->isempty()){
                    $a = $stack->pop();
                    $b = $stack->pop();
                    switch ($arr){
                        case '+':
                            $stack->push($a + $b);break;
                        case '-':
                            $stack->push($a - $b);break;
                        case '*':
                            $stack->push($a * $b);break;
                        case '/':
                            $stack->push($a / $b);break;
                    }
                }
            }
        }
        return $stack->getTop();
    }
}

$stackReverse = new stackReverse();
$arrs = [4, 13, 5, "/", "+"];
$res = $stackReverse->calculate($arrs);
var_dump($res);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值