# [Leetcode 150] Evaluate Reverse Polish Notation

Source: Internet
Author: User

`Import java.util.stack;/** * Evaluate The value of a arithmetic expression in Reverse Polish notation.valid operators is +, -, *, /. Each operand is an integer or another expression. Some examples: ["2", "1", "+", "3", "*"), 1 ["3", "+", "9", "/", "+"] (4 + (13/5))-&G T 6 * */public class Evaluatereversepolishnotation {//20/20 test Cases passed.//status:accepted//runtime:346 Ms//Submitt ed:0 minutes ago//time complexity O (n) space complexity O (n) public int evalrpn (string[] tokens) {stack<integer> Stack = NE        W stack<integer> (); for (String s:tokens) {if (IsOp (s)) {Stack.push (Operation (Stack.pop (), S, Stack.pop ()));} else {Stack.push (Stringtoint (    s));}}    return Stack.pop (); }//Determines whether the operator public boolean isOp (String s) {if (S.equals ("+") | | S.equals ("-") | | S.equals ("*") | |    S.equals ("/")) {return true;    } else {return false;} }//Operator operation on two numbers public int operation (int b, String s, int a) {IF (s.equals ("+")) return a + B;    else if (s.equals ("-")) return a-B;    else if (s.equals ("*")) return a * b;    else return a/b;    }//Converts a numeric string into a digital public int stringtoint (string s) {int n = 0;    int flag = 1; For (Character C:s.tochararray ()) {//determine if the negative if (c = = '-') {flag = -1;continue;}    n = ten * n + C-' 0 ';}    return flag * N; }public static void Main (string[] args) {//TODO auto-generated method stub}}`

[Leetcode 150] Evaluate Reverse Polish Notation

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.