# Lintcode Medium title: Evaluate Reverse Polish notation inverse Polish expression evaluation

Source: Internet
Author: User
Tags lintcode

Topic

Inverse Polish expression evaluation

In the inverse Polish notation, its valid operational symbols include,, `+` `-` `*` , `/` . Each operand can be an integer, or it can be another inverse Polish count expression.

Sample Example
``["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6``
Description

What is an inverse Polish expression?

• Http://en.wikipedia.org/wiki/Reverse_Polish_notation

Solving

Using stacks to solve problems

When the number is in the stack, when it is not the number of two consecutive stack, the data out of the stack operation, the results of the operation into the stack

How to determine a number?

According to the regular or relatively simple

`True`

It is important to note that the case is negative

`True`

I was first to determine if the first character was "-" and then whether it was a number.

The latter is for the four to run simply, note the illegal characters, and in addition to the 0 case, I am directly returning the maximum value.

Another note is that when the stack is empty during the operation, the input is not valid. Inverse Polish expression

` Public classSolution {/**     * @paramtokens the Reverse Polish Notation *@returnThe value*/     Public intEVALRPN (string[] tokens) {//Write Your code here        if(Tokens = =NULL)            return0; if(Tokens.length = = 1)            returnInteger.valueof (tokens[0]); Stack<Integer> stack =NewStack<integer>();  for(intI =0;i< tokens.length; i++) {String str=Tokens[i]; if(Str.matches ("\\d+") = =true||str.substring (0,1). Equals ("-") &&str.substring (1,str.length ()). Matches ("\\d+") = =true){                intnum =integer.valueof (str);            Stack.push (num); }Else{                if(Stack.empty ()) {System.out.println ("The Stack is empty"); return-1; }                intnum2 =Stack.pop (); intNUM1 =Stack.pop (); intres =Calculate (NUM1,NUM2,STR);            Stack.push (RES); }        }        returnStack.pop (); }     Public intCalculateintNUM1,intnum2,string symbol) {        if(Symbol.equals ("+"))            returnnum1+num2; if(Symbol.equals ("-"))            returnNUM1-num2; if(Symbol.equals ("*"))            returnNum1*num2; if(Symbol.equals ("/")){            if(num2!=0){                returnnum1/num2; }Else{                returnInteger.max_value; }        }Else{            returnInteger.max_value; }    }    }`
Java Code

Total time: 11103 ms

Python program appears 1/-123 equals-1 problem, said manual resolution is too stupid, the program is as follows

`classSolution:#@param {string[]} tokens the Reverse Polish Notation    #@return {int} The value    defEvalrpn (self, tokens):#Write Your code here        ifTokens = =None:return0ifLen (tokens) = = 1:            returnInt (tokens[0]) stack= []        #Print 13//3        #Print 13/3        #Print 6/(135) 0        #print 6/( -135)-1         fortkinchTokens:ifTk.isdigit ()orTk[0] = ='-'  andTk[1:].isdigit (): stack.append (int (TK))Else: num2=Stack.pop () num1=stack.pop () Num=self.calculate (NUM1,NUM2,TK) stack.append (num)if(len (tokens) = = 13):                PrintStackreturnStack.pop ()defCalculate (self,num1,num2,sign):ifSign = ='-':            returnNUM1-num2elifSign = ='+':            returnNUM1 +num2elifSign = ='*':            returnNUM1 *num2elifSign = ='/':            ifnum2!=0:return-num1//num2Else:                return0Else:            return0`
Python Code

Lintcode Medium title: Evaluate Reverse Polish notation inverse Polish expression evaluation

Related Keywords:

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.