-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevalRevPolishNotation.js
69 lines (64 loc) · 2.08 KB
/
evalRevPolishNotation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 150 Evaluate Reverse Polish Notation
// https://leetcode.com/problems/min-stack/
// sol 1
const evalRPN = (tokens) => {
const stack = [];
for(let i=0; i<tokens.length; i++) {
if(!isNaN(tokens[i])) {
stack.push(tokens[i])
} else if(isNaN(tokens[i])) {
switch(tokens[i]) {
case '+': {
const num1 = stack.pop();
const num2 = stack.pop();
stack.push(parseInt(num1)+parseInt(num2));
break
}
case '-': {
const num1 = stack.pop();
const num2 = stack.pop();
stack.push(parseInt(num2)-parseInt(num1));
break
}
case '*': {
const num1 = stack.pop();
const num2 = stack.pop();
stack.push(parseInt(num1)*parseInt(num2));
break
}
case '/': {
const num1 = stack.pop();
const num2 = stack.pop();
const res = Math.trunc(parseInt(num2)/parseInt(num1));
stack.push(res === -0 ? 0: res);
break
}
}
}
}
return stack.pop()
}
// console.log(evalRPN(["2", '1','+','3','*']))
// console.log(evalRPN(["4","13","5","/","+"]))
console.log(evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))
// sol 2
const evalRPN1 = (tokens) => {
const stack = [];
const operators = {
"+": (num1, num2) => num1+num2,
"-": (num1, num2) => num1-num2,
"*": (num1, num2) => num1*num2,
"/": (num1, num2) => Math.trunc(num1/num2),
}
for(let i=0; i<tokens.length; i++) {
const token = tokens[i];
if(operators[token]) {
let num2 = stack.pop();
let num1 = stack.pop();
stack.push(operators[token](num1, num2))
} else {
stack.push(parseInt(token))
}
}
return stack.pop();
}