leetcode_Evaluate Reverse Polish Notation

描述:

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

思路:

1.就像题目所描述的,计算逆波兰数学表达式的结果,循环访问字符串数组类型的表达式

2.遍历的字符是数字,将数字进栈,是数学符号的时,将连续的两个数字出栈并计算出结果,然后将结果再入栈

3.重复步骤2直至所有的数组元素被访问完毕。

代码:

//代码通俗易懂,略冗余。

public int evalRPN(String[] tokens) {
        if(tokens==null||tokens.length==0)
            return 0;
        if(tokens.length==1)
            return Integer.parseInt(tokens[0]);
        int parseNum=0;
        int num1=0,num2=0;
        Stack<Integer>st=new Stack<Integer>();
        for(int i=0;i<tokens.length;i++)//iterate the item of the array
        {
        	if(isOperator(tokens[i]))//if the item is operator,caculate the numbers
        	{
        		num2=st.peek();
        		st.pop();
        		num1=st.peek();
        		st.pop();
        		parseNum=evaluteNums(num1,num2,tokens[i]);
        		if(i+1==tokens.length)
        			return parseNum;
        		st.push(parseNum);
        	}else {
				st.push(Integer.parseInt(tokens[i]));//if the item is number,push to the stack
			}

        }
        return parseNum;
    }
	public int evaluteNums(int num1,int num2,String operator)
	{
		if(operator.equals("+"))
			return num1+num2;
		else if(operator.equals("-"))
			return num1-num2;
		else if(operator.equals("*"))
			return num1*num2;
		else
			return num1/num2;
	}
	public boolean isOperator(String str)
	{
		char operator=str.charAt(0);
		if(operator=='+'||operator=='-'||operator=='*'||operator=='/')
		{
			if(str.length()==1)
				return true;
		}
		return false;
	}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 23:28:50

leetcode_Evaluate Reverse Polish Notation的相关文章

leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation

leetcode 逆波兰式求解 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", "

[LeetCode]Evaluate Reverse Polish Notation

题目:Evaluate Reverse Polish Notation 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", &quo

LeetCode150 Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation.  (Medium) Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Example ["2", "1", "+", "3", "*"] -> ((2

lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]

Evaluate Reverse Polish Notation——LeetCode

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", "*"] -&g

【Leetcode】Evaluate Reverse Polish Notation JAVA

   一.问题描述 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", "*&

[Leetcode] evaluate reverse polish notation 计算逆波兰表达式

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", "*"] -> (

Evaluate Reverse Polish Notation(堆栈)

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", "*"] -&g