Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6615883

Browse files
150. Evaluate Reverse Polish Notation
1 parent e930b89 commit 6615883

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
'''
2+
150. Evaluate Reverse Polish Notation
3+
4+
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
5+
6+
Evaluate the expression. Return an integer that represents the value of the expression.
7+
8+
Note that:
9+
10+
The valid operators are '+', '-', '*', and '/'.
11+
Each operand may be an integer or another expression.
12+
The division between two integers always truncates toward zero.
13+
There will not be any division by zero.
14+
The input represents a valid arithmetic expression in a reverse polish notation.
15+
The answer and all the intermediate calculations can be represented in a 32-bit integer.
16+
17+
18+
Example 1:
19+
20+
Input: tokens = ["2","1","+","3","*"]
21+
Output: 9
22+
Explanation: ((2 + 1) * 3) = 9
23+
Example 2:
24+
25+
Input: tokens = ["4","13","5","/","+"]
26+
Output: 6
27+
Explanation: (4 + (13 / 5)) = 6
28+
Example 3:
29+
30+
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
31+
Output: 22
32+
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
33+
= ((10 * (6 / (12 * -11))) + 17) + 5
34+
= ((10 * (6 / -132)) + 17) + 5
35+
= ((10 * 0) + 17) + 5
36+
= (0 + 17) + 5
37+
= 17 + 5
38+
= 22
39+
40+
41+
Constraints:
42+
43+
1 <= tokens.length <= 104
44+
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].
45+
'''
46+
47+
# Brute Force (without stack)
48+
# Time Complexity: O(n^2)
49+
# Space Complexity: O(n)
50+
51+
'''
52+
In this approach, we are going to iterate through the tokens and check if the token is an operator or not.
53+
if token is an operand then our op will be that toekn[i].
54+
55+
A will token[i-2] and B will be token[i-1].
56+
Then we will check the operator and perform the operation.
57+
We will mutate the tokens list and remove the operands and operator from the list.
58+
We will store the result in the tokens list and update the pointer to 0 else we will increment the pointer by 1.
59+
We will repeat this process until we have only one element in the tokens list.
60+
61+
Then we will return the first element of the tokens list.
62+
We will convert the result to int and return it.
63+
'''
64+
65+
class Solution:
66+
def evalRPN(self, tokens):
67+
res = None
68+
69+
i=0
70+
while len(tokens)>1:
71+
if tokens[i] in "*+-/":
72+
op = tokens[i]
73+
a = int(tokens[i - 2])
74+
b = int(tokens[i - 1])
75+
76+
77+
if op == "*":
78+
res = a*b
79+
elif op == "-":
80+
res = a-b
81+
elif op == "+":
82+
res = a+b
83+
elif op == "/":
84+
res = int(a/b)
85+
86+
tokens = tokens[:i - 2] + [str(res)] + tokens[i + 1:]
87+
i=0
88+
else:
89+
i+=1
90+
91+
return int(tokens[0])
92+
93+
# Optimal Approach (using stack)
94+
# Time Complexity: O(n)
95+
# Space Complexity: O(n)
96+
97+
'''
98+
In this approach we are going to use stack to solve the problem.
99+
We will iterate through the tokens and check if the token is an operator or not.
100+
If it is an operand then we will append it to the stack after converting it to int.
101+
If it is an operator then we will pop the last two elements from the stack and perform the operation.
102+
We will append the result to the stack.
103+
We will repeat this process until we have only one element in the stack.
104+
Then we will return the first element of the stack.
105+
'''
106+
107+
class Solution:
108+
def evalRPN(self, tokens):
109+
stack=[]
110+
res = None
111+
112+
for i in range(len(tokens)):
113+
if tokens[i] not in "+-*/":
114+
stack.append(int(tokens[i]))
115+
116+
else:
117+
a= stack.pop()
118+
b= stack.pop()
119+
if tokens[i] == "+":
120+
res = b + a
121+
elif tokens[i] == "-":
122+
res = b - a
123+
elif tokens[i] == "*":
124+
res = b * a
125+
elif tokens[i] == "/":
126+
res = int(b/a)
127+
stack.append(res)
128+
return stack[0]
129+
130+
131+
obj = Solution()
132+
tokens=["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
133+
print(obj.evalRPN(tokens)) # Output: 22

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /