添加150. 逆波兰表达式求值javaScript版本

This commit is contained in:
qingyi.liu
2021-05-26 17:49:06 +08:00
parent 0bb42237a2
commit 07e8abeccb

View File

@ -197,6 +197,32 @@ func evalRPN(tokens []string) int {
} }
``` ```
javaScript:
```js
/**
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function(tokens) {
const s = new Map([
["+", (a, b) => a * 1 + b * 1],
["-", (a, b) => b - a],
["*", (a, b) => b * a],
["/", (a, b) => (b / a) | 0]
]);
const stack = [];
for (const i of tokens) {
if(!s.has(i)) {
stack.push(i);
continue;
}
stack.push(s.get(i)(stack.pop(),stack.pop()))
}
return stack.pop();
};
```
----------------------- -----------------------