leetcode150
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的更多相关文章
- leetcode150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [Swift]LeetCode150. 逆波兰表达式求值 | Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode150:Evaluate Reverse Polish Notation
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- LeetCode150 逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- 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 ...
- Why Python's Integer Division Floors ---- python int(6/-132)时答案不一致,向下取整
leetcode150题中有一个步骤: int(6/-132) == 0 or ==-1? 在自己本地python3环境跑是int(6/-132) =0,但是提交的时候确实-1. 查找相关资料解惑: ...
- LeetCode通关:栈和队列六连,匹配问题有绝招
刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...
随机推荐
- 【BZOJ1703】【usaco2007margold】ranking the cows 奶牛的魅力排名
想的时间比较长所以看题解了= = 原题: Fj有N(N<=1000)头牛,每头牛都有独一无二的正整数 魅力值,Fj想让他们按 魅力值排序. Fj已经知道M(1<=M<=10000)对 ...
- python 二维list取列
b = [i[0] for i in a] # 从a中的每一行取第一个元素.
- 剑指offer-青蛙变态跳台阶-全概率公式
- package.json 文件中的版本号
版本号,格式:"主要版本,次要版本,补丁版本" 指定版本:比如1.2.2,遵循"主版本,次要版本,补丁版本"的格式规定,安装时只安装指定版本. 波浪号(tild ...
- PHP解析xml文件时报错:I/O warning : failed to load external entity
在代码顶部增加 libxml_disable_entity_loader(false); libxml_disable_entity_loader()作用是设置是否禁止从外部加载XML实体,设为tru ...
- 【python】列表&&元组&&字典
列表:用“[]”包裹,可对值增删改. 列表遍历: 方法一: alist=["a","b","c","d","e ...
- django orm 常用查询筛选
大于.大于等于 __gt 大于 __gte 大于等于 User.objects.filter(age__gt=10) // 查询年龄大于10岁的用户 User.objects.filter(age__ ...
- linux 信号处理 一 (基本概念)
信号是Linux编程中非常重要的部分,本文将详细介绍信号机制的基本概念.Linux对信号机制的大致实现方法.如何使用信号,以及有关信号的几个系统调用. 信号机制是进程之间相互传递消息的一种方法,信号全 ...
- C++进阶--const和函数(const and functions)
// const和函数一起使用的情况 class Dog { int age; string name; public: Dog() { age = 3; name = "dummy&quo ...
- 【Hibernate学习笔记-4】在hibernate.cfg.xml中配置C3P0数据源
jar包 hibernate.cfg.xml <?xml version="1.0" encoding="GBK"?> <!DOCTYPE h ...