Leetcode 150. Evaluate Reverse Polish Notation || Code + Explanation + Example Walkthrough
HTML-код
- Опубликовано: 8 сен 2024
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, and /. Each operand may be an integer or another expression.
Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Link to challenge : leetcode.com/e...
very nice explanation you deserve more than this 🙏🙏🙏
Thank you so much
Thank you soo much in a single attempt i have done this.Thanks soo much
Nice Explanation!
Great Explanation 💝
Thanks
Use "long long int" instead of "int" those who are getting RUNTIME ERROR
TY🥲
can we use simple for loop..i am getting run time error in that..??
you are a gem!
Very Nice Explanation
thank you
terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi err is showing
you're the best di!
To the point!
Thanks !!
Thanks☺
Alisha.maam good 👍👌👏
Nice explanation but its saying wrong answer for test case 2: ["4","13","5","/","+"]. My answer is 15 but expected is 6
what datatype are you using ?? after pushing 13 and 5, we encounter '/' so result of 13/5 will be 2 if datatype of ans is int
proof of correctness?
I will appreciate ur silence
sexy bhai
i am doing this code in java and it is giving an empty stack exception I have added the code below please help
class Solution {
public int evalRPN(String[] tokens) {
Stack st=new Stack();
if(tokens.length==0)
return 0;
for(String s:tokens)
{
if(s.equals("+"))
{
int a=st.peek();
st.pop();
int b=st.peek();
st.pop();
st.push(a+b);
}
else if(s.equals("-"))
{
int a=st.peek();
st.pop();
int b=st.peek();
st.pop();
st.push(a-b);
}
else if(s.equals("*"))
{
int a=st.peek();
st.pop();
int b=st.peek();
st.pop();
st.push(a*b);
}
else if(s.equals("/"))
{
int a=st.peek();
st.pop();
int b=st.peek();
st.pop();
if(a>b &&b!=0)
st.push(a/b);
else if(b>a && a!=0)st.push(b/a);
}
else
{
int a=Integer.parseInt(s);
st.push(a);
}
}
return st.pop();
}
}
st.top(); not pop
@@rishabnegi2334 nhi bhai java main pop hi hota hai
int place of integer take long and long.parselong(s);
public int evalRPN(String[] tokens) {
Stack st=new Stack();
if(tokens.length==0)
return 0;
for(String s:tokens)
{
if(s.equals("+"))
{
long a=st.peek();
st.pop();
long b=st.peek();
st.pop();
st.push(a+b);
}
else if(s.equals("-"))
{
long a=st.peek();
st.pop();
long b=st.peek();
st.pop();
st.push(b-a);
}
else if(s.equals("*"))
{
long a=st.peek();
st.pop();
long b=st.peek();
st.pop();
st.push(a*b);
}
else if(s.equals("/"))
{
long a=st.peek();
st.pop();
long b=st.peek();
st.pop();
// if(a>b &&b!=0)
st.push(b/a);
}
else
{
long a=Long.parseLong(s);
st.push(a);
}
}
Long c=new Long(st.peek());
Integer v=c.intValue();
return v;
}
corrected
thanks