新增(0150.逆波兰表达式求值.md):php版本

This commit is contained in:
SevenMonths
2022-05-31 15:56:00 +08:00
parent 474ae02289
commit ab5b82969f

View File

@ -326,5 +326,29 @@ func evalRPN(_ tokens: [String]) -> Int {
}
```
PHP
```php
class Solution {
function evalRPN($tokens) {
$st = new SplStack();
for($i = 0;$i<count($tokens);$i++){
// 是数字直接入栈
if(is_numeric($tokens[$i])){
$st->push($tokens[$i]);
}else{
// 是符号进行运算
$num1 = $st->pop();
$num2 = $st->pop();
if ($tokens[$i] == "+") $st->push($num2 + $num1);
if ($tokens[$i] == "-") $st->push($num2 - $num1);
if ($tokens[$i] == "*") $st->push($num2 * $num1);
// 注意处理小数部分
if ($tokens[$i] == "/") $st->push(intval($num2 / $num1));
}
}
return $st->pop();
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>