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. ACM常用算法

    数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 一维线段树 二维 ...

  2. $.grep()的用法

    grep()方法用于数组元素过滤筛选 grep(array,callback,invert) array:待过滤数组; callback:处理数组中的每个元素,并过滤元素,该函数中包含两个参数,第一个 ...

  3. BLE 4.1 和 BLE 4.2

    BLE 4.2 比 BLE4.1 多了一些新的特性. Low-power IP (IPv6/6LoWPAN) Bluetooth Smart Internet Gateways (GATT) http ...

  4. sed 给文件每行末尾追加相同字符

    给文件file1每行末尾追加字符 ; sed 's/$/;/' file1

  5. HBase的Scan

    HBase的Scan和Get不同,前者获取数据是串行,后者则是并行:是不是有种大跌眼镜的感觉? Scan有四种模式:scan,(Table)snapScan,(Table)scanMR,snapsho ...

  6. java数组变量

    数组变量是一种引用类型的变量,能够指向数组对象.数组对象存储在堆内存中,当数组变量为局部变量时存储在栈内存中. int[] p = new int[]{5, 6, 7, 8, 9}; p是数组变量,指 ...

  7. JS判断IP的正则表达式

    <html> <head> <title>最简洁的IP判断正则表达式</title> <meta http-equiv="Content ...

  8. XML-RPC简单使用

    RPC(Remote Procedure Call)即远程方法调用,是一种在本地的机器上调用远端机器上的一个过程(方法)的技术.这个过程也被大家称为“分布式计算”,是为了提高各个分立机器的“互操作性” ...

  9. Spring IOC - 控制反转(依赖注入) - 懒加载机制

    懒加载机制 Spring默认会在容器初始化的过程中,解析xml,并将单例的bean创建并保存到map中,这样的机制在bean比较少的时间问题不大,但一旦bean非常多时,Spring需要在启动的过程中 ...

  10. 部署DNS从服务器

    修改主服务器中区域信息文件: # vi /etc/named.rfc1912.zones 检查配置文件是否有错误 # named-checkconf 重启named服务程序,让配置文件生效 #syst ...