evaluate-reverse-polish-notation java code
2017-10-15 09:12
387 查看
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
import java.util.Stack; public class Solution { public int evalRPN(String[] tokens) { if(tokens.length==0) return 0; Stack<Integer> stack = new Stack<Integer>(); for(int i=0;i<tokens.length;i++){ if(tokens[i].equals("+")||tokens[i].equals("-")||tokens[i].equals("*")||tokens[i].equals("/")){ if(tokens[i].equals("+")){ int temp; int a = stack.pop(); int b = stack.pop(); temp=a+b; stack.push(temp); } if(tokens[i].equals("-")){ int temp; int b = stack.pop(); int a = stack.pop(); temp=a-b; stack.push(temp); } if(tokens[i].equals("*")){ int temp; int b = stack.pop(); int a = stack.pop(); temp=a*b; stack.push(temp); } if(tokens[i].equals("/")){ int temp; int b = stack.pop(); int a = stack.pop(); temp=a/b; stack.push(temp); } }else{ stack.push(Integer.parseInt(tokens[i])); } } return stack.peek(); } }
相关文章推荐
- Evaluate Reverse Polish Notation ---Java
- [Leetcode][JAVA] Evaluate Reverse Polish Notation
- 【LeetCode-面试算法经典-Java实现】【151-Evaluate Reverse Polish Notation(计算逆波兰式)】
- 【LeetCode刷题Java版】Evaluate Reverse Polish Notation(计算逆波兰表达式)
- leecode 解题总结:150. Evaluate Reverse Polish Notation
- Java实现逆波兰表达式(Evaluate Reverse Polish Notation)
- 计算后续表达式 LectCode之Evaluate Reverse Polish Notation
- Leetcode 150:Evaluate Reverse Polish Notation(计算逆波兰表达式) --java实现
- Java Evaluate Reverse Polish Notation(逆波兰表达式)
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
- Evaluate Reverse Polish Notation (Java)
- Java Evaluate Reverse Polish Notation(逆波兰式)
- leedcode做题总结,题目Evaluate Reverse Polish Notation 2013/11/27
- LeetCode 150 — Evaluate Reverse Polish Notation(C++ Java Python)
- Java for LeetCode 150 Evaluate Reverse Polish Notation
- 【leetcode】Evaluate Reverse Polish Notation 计算后缀表达式的值(JAVA)
- Evaluate Reverse Polish Notation leetcode java
- java常用算法之逆波兰表达式(Evaluate Reverse Polish Notation)
- Leetcode-Evaluate Reverse Polish Notation-java
- Evaluate Reverse Polish Notation leetcode java