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. 【HAOI2014】贴海报

    弱省中的弱省……原题: Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙.张贴规则如下:1 ...

  2. C语言运算符优先级和ASCII表

    1. C语言运算符优先级及结合性 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 -- () 圆括号 (表达式)/函数名(形参表) -- . 成 ...

  3. 12 Factor CLI Apps

    CLIs are a fantastic way to build products. Unlike web applications, they take a small fraction of t ...

  4. FastAdmin 开发第一天:了解 FastAdmin 框架

    了解 FastAdmin 框架 后端组件 ThinkPHP 5 EasyWeChat qr-code 前端组件 AdminLTE bootstrap bootstrap-table jquery la ...

  5. mave安装配置

    首先从官网上 http://maven.apache.org/ 下载最新版Maven.我用的是apache-maven-3.0.4-bin.tar.gz.将下载后的文件拷贝到 /usr/local/目 ...

  6. tomcat源码阅读之StandardHost和StandardEngine

    StandardHost及UML类图: 1.StandardHost类是Host接口的默认实现:其继承自ContainerBase类,说明他也是一个容器类,既然是容器类,那肯定也有管道对象PipeLi ...

  7. oracle之 变更OS时间对数据库的影响

    本文:说明提供了操作系统日期变更对数据库.应用程序数据和作业的影响. 1.它将会影响插入的任何记录,如果涉及到sysdate,则更改日期.2.它还会影响在那个日期运行的任何调度器作业. 如果将系统时间 ...

  8. java 设计模式:单例模式

    Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建自己的唯 ...

  9. 共享设置及ftp设置

    第一部分 共享设置 一.添加编译选项 network---file transfer---aria2                                                   ...

  10. json格式字符串处理

    public class InternalClass         {             public int MID;             public string Name;     ...