forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0150-evaluate-reverse-polish-notation.c
51 lines (41 loc) · 1.19 KB
/
0150-evaluate-reverse-polish-notation.c
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
int evalRPN(char ** tokens, int tokensSize){
long int stk[tokensSize];
int stkIndex = -1;
for(int i = 0; i < tokensSize; i++)
{
if(strcmp(tokens[i], "+") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];
stk[stkIndex] = first + second;
}
else if(strcmp(tokens[i], "-") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];
stk[stkIndex] = second - first;
}
else if(strcmp(tokens[i], "*") == 0)
{
long first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];
stk[stkIndex] = first * second;
}
else if(strcmp(tokens[i], "/") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];
stk[stkIndex] = second / first;
}
else
{
stkIndex++;
stk[stkIndex] = atoi(tokens[i]);
}
}
return stk[stkIndex];
}