原题地址

基本栈操作。

注意数字有可能是负的。

代码:

 int toInteger(string &s) {
int res = ;
bool negative = s[] == '-' ? true : false; for (int i = negative ? : ; i < s.length(); i++) {
res *= ;
res += s[i] - '';
}
return negative ? -res : res;
} int compute(int a, int b, string sign) {
switch (sign[]) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return -;
}
} int evalRPN(vector<string> &tokens) {
stack<int> st; for (auto t : tokens) {
if (t.length() > || t[] >= '' && t[] <= '')
st.push(toInteger(t));
else {
int b = st.top();
st.pop();
int a = st.top();
st.pop();
st.push(compute(a, b, t));
}
} return st.top();
}

Leetcode#150 Evaluate Reverse Polish Notation的更多相关文章

  1. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  2. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  3. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  4. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

  5. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

  6. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  7. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  8. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  9. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. luigi学习2-在hadoop上运行Top Artists

    一.AggregateArtistsHadoop class AggregateArtistsHadoop(luigi.contrib.hadoop.JobTask): date_interval = ...

  2. scan的filter使用

    本次操作的hbase的t1表的数据是: hbase(main)::> scan 't1' ROW COLUMN+CELL column=f1:age, timestamp=, value= co ...

  3. 360提供的SQL防注入

    <?php class sqlsafe { private $getfilter = "'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\ ...

  4. js 触发select onchange事件

    select 或text的onchange事件需要手动(通过键盘输入)改变select或text的值才能触发,本文为大家介绍下使用js触发select onchange事件select 或text的o ...

  5. java reflect 初始学习 动态加载类

    首先要理解Class类: 在java 的反射中,Class.forName("com.lilin.Office") 使用类的全名,这样获取,不仅仅表示了类的类类型,同时还代表着类的 ...

  6. Android--从相册中选取照片并返回结果

    启动系统相册去选择图片 //从相册中选取的方法 private void selectPhoto(){ Intent intent = new Intent(Intent.ACTION_PICK); ...

  7. Android greenDao的简单配置和使用

    最近自学做东西的时候用到了一个收藏的功能,然后我想把东西存放到SQLite当中,然而自己传值的时候都是用到的实体类,所以存起来也比较麻烦,所以从网上找到一个greenDao的开源框架非常火,不仅效率高 ...

  8. ubuntu server 14.10 安装 nodejs

    apt-get install nodejs 会报错,提示内核版本过低.如果升级,可能遇到提示boot空间不足,要求释放更多空间: sudo aptitude purge ~ilinux-image- ...

  9. OSGi之Bundle

    OSGi提出的根源是什么?在我看来就是对JVM的类加载机制进行了扩展,添加了一系列的规则,使得原有的类包(Class Package)扩展到类域(Class Domain).然后是建立在类域上的一系列 ...

  10. Libevent windows/linux下编译

    1.windows下: 编译环境: windows xp sp3 + vs2010 (1)    解压libevent-2.0.21-stable.tar.gz到D:\libevent-2.0.21- ...