Leetcode Note: Evaluate Reverse Polish Notation (inverse Polish calculation)

Source: Internet
Author: User

Leetcode Note: Evaluate Reverse Polish Notation (inverse Polish calculation)

I. Description

Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +,-, *,/. Each operand may be an integer or another expression.
Some examples:

[2, 1, +, 3, *] -> ((2 + 1) * 3) -> 9

[4, 13, 5, /, +] -> (4 + (13 / 5)) -> 6

Ii. Question Analysis

This question examines the inverse Polish formula, also called the suffix expression (the operator is written after the operand ). Suppose there is an expression E whose suffix format is defined as follows:

If E is a variable or constant, the suffix of E is E itself. If E is an expression in the form of E1 operator E2, how is the operator binary operator, then E is suffixed with E1, E2,
Operator; if E is an expression in the form of (E1), the suffix of E1 is the suffix of E.

An example is as follows:

The following uses(a+b)*cFor example:
(a+b)*cThe inverse polish type isab+c*Assume that the computerab+c*Push the elements from left to right to the stack. Then, based on the operator, the two elements at the top of the stack are pushed out of the stack, and the operation is executed.ab+c*The execution result is as follows:

A inbound stack (0 position); B inbound stack (1 position); operator encountered “+”, Set aAnd bOutput stack, execute a+bTo obtain the result. d=a+b, And then dInbound stack (0 position); c inbound stack (1 position); operator encountered “*”, Set dAnd cOutput stack, execute d*cTo obtain the result. e, And then eInbound stack (0 position ).

After the above calculation, the computer can obtain(a+b)*cCalculation Resulte.

It is very easy to implement the inverse Polish formula, which can be done using stacks. In a program, you must convert integers and strings.

Iii. Sample Code

# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; class Solution {public: int str2int (string s) // convert string to int {int result = 0; int base = 1; int t = 1; // if (s [0] = '-') t =-1; for (int I = s. size ()-1; I> = 0; -- I) {if (s [I]> = '0' & s [I] <= '9 ') {result + = base * (s [I]-'0'); base * = 10 ;}} return result * t;} int evalRPN (vector
     
      
& Tokens) {stack
      
        K; for (int I = 0; I <tokens. size (); ++ I) {if (tokens [I] = + | tokens [I] =-| tokens [I] = * | tokens [I] = /) {int Num2 = k. top (); // The first retrieved is the right operand k. pop (); int Num1 = k. top (); // left operand k. pop (); if (tokens [I] = +) {k. push (Num1 + Num2);} else if (tokens [I] =-) {k. push (Num1-Num2);} else if (tokens [I] = *) {k. push (Num1 * Num2);} else if (tokens [I] ==/) {k. push (Num1/Num2) ;}} else k. push (str2int (tokens [I]);} return k. top (); // The last element left in the stack is the result }};
      
     
    
   
  

Iv. Summary

Although the entire idea is simple, there are still some details during programming, such as two operands popped up from the stack, the first is the right operand, and the second is the left operand; whether to convert string to int. You may need to consider other data types, such as floating point numbers and other boundary conditions...

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.