150. Evaluate Reverse Polish Notation
2018-03-19 00:27
246 查看
该方法使用栈来解决。
public class Solution {
public int evalRPN(String[] tokens) {
int a,b;
Stack<Integer> S = new Stack<Integer>();
for (String s : tokens) {
if(s.equals("+")) {
S.add(S.pop()+S.pop());
}
else if(s.equals("/")) {
b = S.pop();
a = S.pop();
S.add(a / b);
}
else if(s.equals("*")) {
S.add(S.pop() * S.pop());
}
else if(s.equals("-")) {
b = S.pop();
a = S.pop();
S.add(a - b);
}
else {
S.add(Integer.parseInt(s));
}
}
return S.pop();
}
}
相关文章推荐
- Leetcode 150. Evaluate Reverse Polish Notation
- LeetCode *** 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- [leetcode] 【栈】150. Evaluate Reverse Polish Notation
- [LeetCode] 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation(逆波兰式)
- 150. Evaluate Reverse Polish Notation (M)
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation(java)
- 150. Evaluate Reverse Polish Notation
- leetcode-150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- [LeetCode]150.Evaluate Reverse Polish Notation
- leecode 解题总结:150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- leetcode 150.Evaluate Reverse Polish Notation
- LeetCode-150. Evaluate Reverse Polish Notation (JAVA)逆波兰表达式求值
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation