Leetcode 150. Evaluate Reverse Polish Notation || Code + Explanation + Example Walkthrough

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ส.ค. 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...

ความคิดเห็น • 29

  • @abaytekle21
    @abaytekle21 2 ปีที่แล้ว +4

    very nice explanation you deserve more than this 🙏🙏🙏

  • @KRajesh-ej7dy
    @KRajesh-ej7dy 7 หลายเดือนก่อน

    Thank you soo much in a single attempt i have done this.Thanks soo much

  • @manojtate9604
    @manojtate9604 6 หลายเดือนก่อน

    Nice Explanation!

  • @mdimranhosen6674
    @mdimranhosen6674 2 ปีที่แล้ว +2

    Great Explanation 💝

  • @osikkhitogamer2715
    @osikkhitogamer2715 7 หลายเดือนก่อน

    Thanks

  • @shubhamsukum
    @shubhamsukum ปีที่แล้ว +6

    Use "long long int" instead of "int" those who are getting RUNTIME ERROR

  • @anujkanojia1450
    @anujkanojia1450 ปีที่แล้ว +1

    terminate called after throwing an instance of 'std::invalid_argument'
    what(): stoi err is showing

  • @eshaanpandey7353
    @eshaanpandey7353 2 ปีที่แล้ว

    Very Nice Explanation

  • @Rajat_maurya
    @Rajat_maurya 2 ปีที่แล้ว +1

    thank you

  • @falakhasija2813
    @falakhasija2813 2 ปีที่แล้ว

    you are a gem!

  • @bharathbk5474
    @bharathbk5474 ปีที่แล้ว

    you're the best di!

  • @rohitkapade1130
    @rohitkapade1130 ปีที่แล้ว

    thanks

  • @kunalkheeva
    @kunalkheeva 7 หลายเดือนก่อน

    To the point!

  • @raushankumar6993
    @raushankumar6993 ปีที่แล้ว

    Thanks☺

  • @gouravkumarshaw5467
    @gouravkumarshaw5467 ปีที่แล้ว

    Thanks !!

  • @sakshampaliwal8170
    @sakshampaliwal8170 ปีที่แล้ว

    proof of correctness?

  • @none2868
    @none2868 ปีที่แล้ว

    Nice explanation but its saying wrong answer for test case 2: ["4","13","5","/","+"]. My answer is 15 but expected is 6

    • @yashunta4438
      @yashunta4438 9 หลายเดือนก่อน

      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

  • @Idukhan-jj9kc
    @Idukhan-jj9kc 3 ปีที่แล้ว

    Alisha.maam good 👍👌👏

  • @preetikodwani9842
    @preetikodwani9842 3 หลายเดือนก่อน

    I will appreciate ur silence

  • @saiei
    @saiei 6 หลายเดือนก่อน

    sexy bhai

  • @Star_Bawa9
    @Star_Bawa9 ปีที่แล้ว

    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();
    }
    }

    • @rishabnegi2334
      @rishabnegi2334 ปีที่แล้ว

      st.top(); not pop

    • @Star_Bawa9
      @Star_Bawa9 ปีที่แล้ว

      @@rishabnegi2334 nhi bhai java main pop hi hota hai

    • @shivamsinha5554
      @shivamsinha5554 ปีที่แล้ว

      int place of integer take long and long.parselong(s);

    • @shivamsinha5554
      @shivamsinha5554 ปีที่แล้ว

      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