---
title: "leetcode-Evaluate Reverse Polish Notation"
author: "Thomas会Coding"
date: "2015-01-21T12:09:44Z"
tags: []
categories: ["编程语言"]
slug: "leetcode-evaluate-reverse-polish-notation"
draft: false
lastmod: "2015-01-21T12:09:44Z"
id: 2177982
url: "https://zzcjobstudy.iteye.com/blog/2177982"
---

Valid operators are `+`, `-`, `*`, `/`. Each operand may be an integer or another expression.

```
public class Solution {
    public int evalRPN(String[] tokens) {
       int result=0;
		List<Integer> arrayList=new LinkedList<Integer>();
		int temp1=0;
		int temp2=0;
		for(String temp:tokens){
			if(temp.compareTo("+")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2+temp1;
				arrayList.add(arrayList.size(),result);
			}
			else if(temp.compareTo("-")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2-temp1;
				arrayList.add(arrayList.size(),result);
			}else if(temp.compareTo("*")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2*temp1;
				arrayList.add(arrayList.size(),result);
			}else if(temp.compareTo("/")==0){
				temp1=arrayList.remove(arrayList.size()-1);
				temp2=arrayList.remove(arrayList.size()-1);
				result=temp2/temp1;
				arrayList.add(arrayList.size(),result);
			}else{
				arrayList.add(arrayList.size(),Integer.parseInt(temp));
			}
		}
		
		result=arrayList.get(0);
		return result;
    }
}
```

 思路：将数组中的内容遍历，遇见数字就存入list中，遇见符号就弹出，将计算的结果再存入list中。

一开始用temp==“/”老是报runtime error 。换成temp.compareTo("/")==0就Accepted了