-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
46 lines (45 loc) · 1.32 KB
/
Copy pathsolution.java
File metadata and controls
46 lines (45 loc) · 1.32 KB
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
// 150. Evaluate Reverse Polish Notation
// https://leetcode.com/problems/evaluate-reverse-polish-notation/
// Medium | Java | Accepted 2025-10-26
// Runtime 6 ms | Memory 45 MB
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for(int i = 0; i<tokens.length; i++)
{
if(tokens[i].equals("+"))
{
int int1 = stack.pop();
int int2 = (stack.pop());
int2+=int1;
stack.push(int2);
}
else if(tokens[i].equals("-"))
{
int int1 = (stack.pop());
int int2 = (stack.pop());
int2-=int1;
stack.push(int2);
}
else if(tokens[i].equals("*"))
{
int int1 = (stack.pop());
int int2 = (stack.pop());
int2*=int1;
stack.push(int2);
}
else if(tokens[i].equals("/"))
{
int int1 = (stack.pop());
int int2 = (stack.pop());
int2/=int1;
stack.push(int2);
}
else
{
stack.push(Integer.parseInt(tokens[i]));
}
}
return stack.pop();
}
}