mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
新增(0150.逆波兰表达式求值.md):php版本
This commit is contained in:
@ -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>
|
||||
|
Reference in New Issue
Block a user