链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
java代码:
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<Integer>();
for(int i = 0;i<tokens.length;i++)
{
if(tokens[i].equals("+"))
{
int op1 = (int)s.pop();
int op2 = (int)s.pop();
int result = op1+op2;
s.push(result);
}
else if(tokens[i].equals("-"))
{
int op1 = (int)s.pop();
int op2 = (int)s.pop();
int result = op2-op1;
s.push(result);
}
else if(tokens[i].equals("*"))
{
int op1 = (int)s.pop();
int op2 = (int)s.pop();
int result = op1*op2;
s.push(result);
}
else if(tokens[i].equals("/"))
{
int op1 = (int)s.pop();
int op2 = (int)s.pop();
int result = op2/op1;
s.push(result);
}
else
{
int result = Integer.parseInt( tokens[i] );
s.push(result);
}
}
return (int)s.pop();
}
}