public class Solution
{
public int EvalRPN(string[] tokens)
{
Stack<int> ST_NUM = new Stack<int>();
foreach (var to in tokens)
{
if (to == "+" || to == "-" || to == "*" || to == "/")
{
var num1 = ST_NUM.Pop();
var num2 = ST_NUM.Pop();
if (to == "+")
{
var num = num2 + num1;
ST_NUM.Push(num);
}
else if (to == "-")
{
var num = num2 - num1;
ST_NUM.Push(num);
}
else if (to == "*")
{
var num = num2 * num1;
ST_NUM.Push(num);
}
else if (to == "/")
{
var num = num2 / num1;
ST_NUM.Push(num);
}
}
else
{
var num = Convert.ToInt32(to);
ST_NUM.Push(num);
}
}
var result = ST_NUM.Pop();
return result;
}
}

补充一个python的实现:

 import math
class Solution:
def __init__(self):
self.symbollist = set()
self.symbollist.add('+')
self.symbollist.add('-')
self.symbollist.add('*')
self.symbollist.add('/') def isSymbol(self,string):
if string in self.symbollist:
return True
else:
return False
def cal(self,num1,num2,sym):
if sym == '+':
return num1 + num2
elif sym == '-':
return num2 - num1
elif sym == '*':
return num1 * num2
else:
dd = num2 / num1
if dd < :
dd = math.ceil(dd)
else:
dd = math.floor(dd)
return dd def evalRPN(self, tokens: 'List[str]') -> int:
numstack = []
r =
for s in tokens:
if self.isSymbol(s):
num1 = numstack.pop(-)
num2 = numstack.pop(-)
r = self.cal(num1,num2,s)
numstack.append(r)
else:
numstack.append(int(s))
return numstack[]

leetcode150的更多相关文章

  1. leetcode150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  2. [Swift]LeetCode150. 逆波兰表达式求值 | Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. LeetCode150:Evaluate Reverse Polish Notation

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  4. Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...

  5. LeetCode150 逆波兰表达式求值

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...

  6. 124. Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  7. Why Python's Integer Division Floors ---- python int(6/-132)时答案不一致,向下取整

    leetcode150题中有一个步骤: int(6/-132) == 0 or ==-1? 在自己本地python3环境跑是int(6/-132) =0,但是提交的时候确实-1. 查找相关资料解惑: ...

  8. LeetCode通关:栈和队列六连,匹配问题有绝招

    刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...

随机推荐

  1. hdu 5285 二分图黑白染色

    题意:给出 n 个人,以及 m 对互不认识的关系,剩余的人都互相认识,要将所有人分成两组,组内不能有互不认识的人,要求每组至少有一人,并且第一组人数尽量多,问两组人数或不可能时单独输出 BC 48 场 ...

  2. 微软通过.NET Native为Windows Store应用提速

    .NET Native是微软的一次尝试,旨在降低Windows Store应用的启动时间和内存占用. 自从去年11月份,有人发现Windows Store应用的启动速度有了大幅提高后,对该项目的猜测就 ...

  3. 协程、gevent实现异步io、进程、线程、协程对比

    异步io的说白了就是遇到io操作的时候,就停下来去做别的事情.io分网络io和磁盘io,网络io比如说打开一个网站获取数据,下载一首歌等等,磁盘io就是把数据存到一个文件里面,写到磁盘上. 从网站上获 ...

  4. commonJS、AMD、es模块化 区别(表格比较)

    commonJS.AMD.es6模块化 区别(表格比较): table th:first-of-type { } table th:nth-of-type(3) { width: 150px; } t ...

  5. git revert回退时提示One or more files are in a conflicted state

    解决代码冲突 如果commit时出现“You have to update your work copy first.”红色警告,说明版本库中的此文件已经被其他人修改了. 请先点“ok”按钮退出.执行 ...

  6. box-shadow 边框样式

    如下 box-shadow: 0 1px 3px 0 rgba(0,0,0,.2), 0 1px 1px 0 rgba(0,0,0,.14), 0 2px 1px -1px rgba(0,0,0,.1 ...

  7. xml dom minidom

    一. xml相关术语: 1.Document(文档): 对应一个xml文件 2.Declaration(声明): <?xml version="1.0" encoding=& ...

  8. oracle学习操作(1)

    一.oracle表及表空间: 1.查看用户.用户表空间等,需要sysdba登陆: select username, default_tablespace from dba_users;   2.一个数 ...

  9. IE6 CSS高度height:100% 无效解决方法总结

    原文地址:http://www.cnblogs.com/huangyong8585/archive/2013/02/05/2893058.html   上面红色部分为 height:100%; 自动拉 ...

  10. javascript节点操作replaceChild()

    replaceChild(a,b)是用来替换文档中的已有元素的 参数a:要插入的节点, 参数b:要替换的节点 var oDiv = document.getElementById("guoD ...