File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -325,6 +325,33 @@ func evalRPN(_ tokens: [String]) -> Int {
325325 return stack.last !
326326}
327327```
328+
329+
330+ PHP:
331+ ``` php
332+ class Solution {
333+ function evalRPN($tokens) {
334+ $st = new SplStack();
335+ for($i = 0;$i<count ($tokens);$i++){
336+ // 是数字直接入栈
337+ if(is_numeric($tokens[$i])){
338+ $st- >push($tokens[$i]);
339+ }else{
340+ // 是符号进行运算
341+ $num1 = $st->pop();
342+ $num2 = $st->pop();
343+ if ($tokens[$i] == "+") $st->push($num2 + $num1);
344+ if ($tokens[$i] == "-") $st->push($num2 - $num1);
345+ if ($tokens[$i] == "*") $st->push($num2 * $num1);
346+ // 注意处理小数部分
347+ if ($tokens[$i] == "/") $st->push(intval($num2 / $num1));
348+ }
349+ }
350+ return $st->pop();
351+ }
352+ }
353+ ```
354+
328355Scala:
329356``` scala
330357object Solution {
@@ -351,6 +378,7 @@ object Solution {
351378 // 最后返回栈顶,不需要加return关键字
352379 stack.pop()
353380 }
381+
354382}
355383```
356384-----------------------
You can’t perform that action at this time.
0 commit comments