用php完成LeetCode – Evaluate Reverse Polish Notation
来源:互联网 发布:华山 险 知乎 编辑:程序博客网 时间:2024/06/16 03:13
以我写的前一篇的‘关于php中栈的基本操作’ http://blog.csdn.net/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);
0 0
- 用php完成LeetCode – Evaluate Reverse Polish Notation
- LeetCode – Evaluate Reverse Polish Notation
- LeetCode–Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- 【LeetCode】Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation
- MVP架构的简单实现
- 周期串(Periodic Strings,UVa455)
- 精进-如何成为一个高段位的学习者
- 第一章 第二篇
- 【PAT乙级题目1015】德才论
- 用php完成LeetCode – Evaluate Reverse Polish Notation
- PATL2-007. 家庭房产-并查集
- 关于source insight提示source insight can't create file的解决方法
- 无题-李商隐
- error: Microsoft Visual C++ 9.0
- c++自制锁机程序--两行代码
- 近期总结!
- 多线程_如何实现多线程及多线程方式1的思路、多线程方式1的代码实现
- Java入门问题:找不到或无法加载主类