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...

Комментарии • 29

  • @abaytekle21
    @abaytekle21 2 года назад +4

    very nice explanation you deserve more than this 🙏🙏🙏

  • @KRajesh-ej7dy
    @KRajesh-ej7dy 8 месяцев назад

    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 💝

  • @oshikkhitogamer420
    @oshikkhitogamer420 7 месяцев назад

    Thanks

  • @shubhamsukum
    @shubhamsukum Год назад +6

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

  • @falakhasija2813
    @falakhasija2813 2 года назад

    you are a gem!

  • @eshaanpandey7353
    @eshaanpandey7353 2 года назад

    Very Nice Explanation

  • @Rajat_maurya
    @Rajat_maurya 2 года назад +1

    thank you

  • @anujkanojia1450
    @anujkanojia1450 Год назад +1

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

  • @bharathbk5474
    @bharathbk5474 Год назад

    you're the best di!

  • @kunalkheeva
    @kunalkheeva 7 месяцев назад

    To the point!

  • @gouravkumarshaw5467
    @gouravkumarshaw5467 Год назад

    Thanks !!

  • @raushankumar6993
    @raushankumar6993 Год назад

    Thanks☺

  • @Idukhan-jj9kc
    @Idukhan-jj9kc 3 года назад

    Alisha.maam good 👍👌👏

  • @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 10 месяцев назад

      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

  • @sakshampaliwal8170
    @sakshampaliwal8170 Год назад

    proof of correctness?

  • @preetikodwani9842
    @preetikodwani9842 3 месяца назад

    I will appreciate ur silence

  • @saiei
    @saiei 7 месяцев назад

    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

  • @rohitkapade1130
    @rohitkapade1130 Год назад

    thanks