"Leetcode" Evaluate Reverse Polish Notation

Source: Internet
Author: User

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

valid operators Are + ,  -,  * ,  /. Each operand is an integer or another expression.

Some Examples:

  ["2", "1", "+", "3", "*")--((2 + 1) (3)-9  ["4", "", "5", "/", "+"], 4 + (13/5))
Everyone in fact Baidu a bit "inverse polish expression" on it can be ~

(1) First constructs an operator stack, which follows the principle that the higher the top priority of the stack is in the stack. (2) read into a simple arithmetic expression expressed in infix, for convenience, set the right end of the simple arithmetic expression plus the lowest priority special symbol "#". (3) scan from left to right the arithmetic expression, starting from the first character, if the character is a number, then parse to the end of the number string and output the number directly. (4) If it is not a number, the character is an operator, at which time the precedence relationship needs to be compared. This is done as follows: Compares this character to the precedence of the operator at the top of the operator stack. If the character precedence is higher than the operator at the top of this operator stack, the operator is put into the stack. If not, the top operator is popped out of the stack until the top of the stack operator is lower than the current operator and the character is placed on the stack. (5) Repeat the above operation (3)-(4) until the complete simple arithmetic expression is scanned, to make sure that all characters are handled correctly, we can convert the simple arithmetic expression of infix notation into a simple arithmetic expression of inverse polish representation.
Import Java.util.stack;public class Evaluatereversepolishnotation {public static void main (String args[]) { Evaluatereversepolishnotation ERP = new Evaluatereversepolishnotation (); String[] Tokens = {"3", "4", "+"}; System.out.println (ERP.EVALRPN (tokens));}        public int EVALRPN (string[] tokens) {stack<integer> Stack = new stack<integer> ();        int input; for (int i=0;i<tokens.length;i++) {if (Tokens[i].charat (0) >= ' 0 ' &&tokens[i].charat (0) <= ' 9 ') {I        Nput = Integer.parseint (Tokens[i]);        Stack.push (input);        } else if (Tokens[i].length () >1&&tokens[i].charat (0) = = '-') {input = Integer.parseint (Tokens[i]);        Stack.push (input);        } else if (Tokens[i].charat (0) = = ' + ') Stack.push (Stack.pop () +stack.pop ());        else if (tokens[i].charat (0) = = '-') {int tmp = Stack.pop ();        Stack.push (Stack.pop ()-tmp); } else if (Tokens[i].charat (0) = = '/') {int tmp = STACK.POP ();        Stack.push (Stack.pop ()/TMP);        } else if (Tokens[i].charat (0) = = ' * ') Stack.push (Stack.pop () *stack.pop ());    } return Stack.peek (); }}



"Leetcode" Evaluate Reverse Polish Notation

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.