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

Sdílet
Vložit
  • čas přidán 25. 05. 2021
  • 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/explore/challeng...

Komentáře • 29

  • @KRajesh-ej7dy
    @KRajesh-ej7dy Před 6 měsíci

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

  • @falakhasija2813
    @falakhasija2813 Před 2 lety

    you are a gem!

  • @manojtate9604
    @manojtate9604 Před 4 měsíci

    Nice Explanation!

  • @mdimranhosen6674
    @mdimranhosen6674 Před 2 lety +2

    Great Explanation 💝

  • @eshaanpandey7353
    @eshaanpandey7353 Před rokem

    Very Nice Explanation

  • @osikkhitogamer2715
    @osikkhitogamer2715 Před 5 měsíci

    Thanks

  • @abaytekle21
    @abaytekle21 Před 2 lety +4

    very nice explanation you deserve more than this 🙏🙏🙏

  • @Rajat_maurya
    @Rajat_maurya Před 2 lety +1

    thank you

  • @bharathbk5474
    @bharathbk5474 Před rokem

    you're the best di!

  • @gouravkumarshaw5467
    @gouravkumarshaw5467 Před rokem

    Thanks !!

  • @raushankumar6993
    @raushankumar6993 Před rokem

    Thanks☺

  • @kunalkheeva
    @kunalkheeva Před 5 měsíci

    To the point!

  • @rohitkapade1130
    @rohitkapade1130 Před rokem

    thanks

  • @anujkanojia1450
    @anujkanojia1450 Před rokem

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

  • @shubhamsukum
    @shubhamsukum Před rokem +6

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

  • @sakshampaliwal8170
    @sakshampaliwal8170 Před rokem

    proof of correctness?

  • @Idukhan-jj9kc
    @Idukhan-jj9kc Před 3 lety

    Alisha.maam good 👍👌👏

  • @none2868
    @none2868 Před rokem

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

    • @yashunta4438
      @yashunta4438 Před 8 měsíci

      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

  • @preetikodwani9842
    @preetikodwani9842 Před 2 měsíci

    I will appreciate ur silence

  • @saiei
    @saiei Před 5 měsíci

    sexy bhai

  • @Star_Bawa9
    @Star_Bawa9 Před rokem

    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 Před rokem

      st.top(); not pop

    • @Star_Bawa9
      @Star_Bawa9 Před rokem

      @@rishabnegi2334 nhi bhai java main pop hi hota hai

    • @shivamsinha5554
      @shivamsinha5554 Před rokem

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

    • @shivamsinha5554
      @shivamsinha5554 Před rokem

      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