#Leet Code# Evaluate Reverse Polish Notation
描述:计算逆波兰表达法的结果
Sample:
["", "", "+", "", "*"] -> ((2 + 1) * 3) -> 9
["", "", "", "/", "+"] -> (4 + (13 / 5)) -> 6
使用stack实现:
def is_op(c):
return c in ['+', '-', '*', '/'] def divide(x, y):
if (x * y) < 0:
return -1 * ((-x)/y)
return x/y class Solution:
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
opDict = {'+': lambda x,y: x+y,
'-': lambda x,y: x-y,
'*': lambda x,y: x*y,
'/': divide}
record = [] for item in tokens:
if is_op(item):
second = record.pop()
first = record.pop()
record.append(opDict[item](first, second))
else:
record.append(int(item))
return record[0]
使用树实现:
def is_op(c):
return c in ['+', '-', '*', '/'] def divide(x, y):
if (x * y) < 0:
return -1 * ((-x)/y)
return x/y class Tree:
def __init__(self, data):
self.data = data
self.parent = None
self.left = None
self.right = None class Solution:
# @param tokens, a list of string
# @return an integer
def __init__(self):
self.opDict = {'+': lambda x,y: x+y,
'-': lambda x,y: x-y,
'*': lambda x,y: x*y,
'/': divide} def builtTree(self, tokens):
if not is_op(tokens[-1]):
return int(tokens[-1]) # if element is an operator
cur_tree = Tree(tokens[-1])
top_tree = cur_tree for item in tokens[-2::-1]:
if cur_tree.right is None:
if is_op(item):
cur_tree.right = Tree(item)
cur_tree.right.parent = cur_tree
cur_tree = cur_tree.right
else:
cur_tree.right = int(item) if cur_tree.right and cur_tree.left:
cur_tree = self.getUpperNode(cur_tree)
continue if cur_tree.left is None:
if is_op(item):
cur_tree.left = Tree(item)
cur_tree.left.parent = cur_tree
cur_tree = cur_tree.left
else:
cur_tree.left = int(item) if cur_tree.right is not None and cur_tree.left is not None:
cur_tree = self.getUpperNode(cur_tree) return top_tree # Move to upper node if cur node if full. If top_node return.
def getUpperNode(self, node):
while node.right is not None and node.left is not None:
if node.parent is None:
return node node = node.parent return node def getValue(self, node):
if type(node) is type(1):
return node
else:
return self.getResult(node) def getResult(self, treeNode):
leftValue = self.getValue(treeNode.left)
rightValue = self.getValue(treeNode.right) result = self.opDict[treeNode.data](leftValue, rightValue) return result def evalRPN(self, tokens):
topNode = self.builtTree(tokens) if type(topNode) is type(1):
return topNode
else:
resultNum = self.getResult(topNode)
return resultNum
备注-1:if cur_node.right is None 不能用 if cur_node.right 因为cur_node.right 如果是数字0的话会有问题 当然 不转成int的话直接存string等到运算时再转int应该就可以这样写了
备注-2:python的除法跟c++不太一样 3/-5 = -1
结论:
根据问题的具体特性,选择合适的数据结构解决问题会差别很大
#Leet Code# Evaluate Reverse Polish Notation的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
随机推荐
- 每天进步一点达——MySQL——myisampack
一. 简单介绍 myisampack是一个压缩使用MyISAM引擎表的工具,通常会压缩40%~70%,当须要訪问数据.server会将所须要的信息读入到内存中.所以当訪问详细记录时,性能 ...
- Wpf OpenFileDialog
Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialo ...
- careercup-数学与概率 7.7
7.7 有些数的素因子只有3.5.7,请设计一个算法,找出其中第k个数. 解法: 首先,我们可以将满足条件的前几个数列出来,以此寻找解题思路. 一种简单的思路就是对于已经列出的数,我们依次去乘以3,5 ...
- Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置
QtreeView是ui中最常用的控件,Qt中QTreeWidget比QTreeView更简单,但没有QTreeView那么灵活(QTreeWidget封装的和MFC的CTreeCtrl很类似,没有m ...
- 合并两个vectcor——2013-08-26
vector<int> v1; vector<int> v2; for(int i=0; i<5; i++) { v1.push_back(i); if(i%2==1) ...
- 使用Netty进行Android与Server端通信实现文字发送接收与图片上传
ANOTHER TITLE: Let’s use netty to achieve text send and receive and image transfer to server based ...
- winform中的Dock属性问题
经过测试发现,winform中的Dock属性,先添加的控件,比后添加的控件的Dock权重要高.系统会优先显示先添加控件的Dock属性,再显示后添加的Dock属性. ------------------ ...
- Flume简介与使用(一)——Flume安装与配置
Flume简介与使用(一)——Flume安装与配置 Flume简介 Flume是一个分布式的.可靠的.实用的服务——从不同的数据源高效的采集.整合.移动海量数据. 分布式:可以多台机器同时运行采集数据 ...
- Java 内存分析图
client -------------------------- public class Client{ public static void main(String[] args){ Perso ...
- js局部变量与全局变量
在最外层定义的是全局变量 如果在函数内部不用var声明直接赋值的变量,那么这个变量也是全局变量 在函数内部用var声明的变量叫做局部变量 定义在最开头的全局变量在整个js范围内都可以访问到,都可以使用 ...