Leetcode Oj reverse polish notation
来源:互联网 发布:夜访吸血鬼影评 知乎 编辑:程序博客网 时间:2024/06/04 20:16
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
Solution using Python
Pay special attention to division in python 2.7
class Solution: # @param tokens, a list of string # @return an integer def evalRPN(self, tokens): stack = [] for i in range(0,len(tokens)): if tokens[i]=="+": a = stack.pop() b = stack.pop() c= int(a)+int(b) stack.append(str(c)) elif tokens[i]=="-": a = stack.pop() b = stack.pop() c= int(b)-int(a) stack.append(str(c)) elif tokens[i]=="*": a = stack.pop() b = stack.pop() c= int(a)*int(b) stack.append(str(c)) elif tokens[i]=="/": a = stack.pop() b = stack.pop() if int(a) * int (b) <0 and abs(int(b)) %abs(int(a)) !=0: c= int(b)/int(a) +1 else: c= int(b)/int(a) stack.append(str(c)) else: stack.append(tokens[i]) return int(stack.pop())
0 0
- Leetcode Oj reverse polish notation
- LeetCode OJ:Evaluate Reverse Polish Notation
- Leetcode OJ: Evaluate Reverse Polish Notation
- LeetCode OJ Evaluate Reverse Polish Notation
- LeetCode OJ --问题与解答 Evaluate Reverse Polish Notation
- [LeetCode OJ] Evaluate Reverse Polish Notation 解题报告
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Android中读写XML的方法
- Layout
- Android strings.xml文件中提供占位符来格式化数据
- eBay User feedback clustering in R
- flex:嵌入应用程序资源
- Leetcode Oj reverse polish notation
- 重编译LINUX内核调整SCTP参数的方法
- VC常见数据类型转换详解
- android开发 NDK编译和使用静态库、动态库
- ORACLE的简单触发器实现
- Android Studio 快捷键汇总
- ZigZag Conversion
- java环境变量 jdk jre jvm区别 java_home path设置 总结
- PL/SQL 导出导出