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

Valid operators are +-*/. Each operand may be an integer or another expression.

Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 此题是求解后缀表达式,题目比较简单,开一个数据stack即可,此题的改进版本是引入括号,此时要开一个数据stack和运算符stack
bool isOperator(string& op){
if(op == "+" || op == "-" || op == "*" || op == "/") return true;
else return false;
} int calc(int a, int b, char op){
switch(op){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
} int evalRPN(vector<string> &tokens){
stack<int> num;
for(int i = ; i < tokens.size(); ++ i){
if(!isOperator(tokens[i])){
num.push(atoi(tokens[i].c_str()));
}else{
int num2 = num.top(); num.pop();
int num1 = num.top(); num.pop();
char op = tokens[i][];
num.push(calc(num1,num2,op));
}
}
return num.top();
}
 

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

  1. LeetCode: Evaluate Reverse Polish Notation 解题报告

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

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

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

  3. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  4. [leetcode]Evaluate Reverse Polish Notation @ Python

    原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...

  5. leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

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

  6. [LeetCode] Evaluate Reverse Polish Notation stack 栈

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

  7. [LeetCode] Evaluate Reverse Polish Notation [2]

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

  8. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  9. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

随机推荐

  1. Generic Access Profile

    转自:https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile ​​Assigned numbe ...

  2. JavaScript 火花效果

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  3. python列表分组的技巧

    今天项目上需要到的. 如,将并行部署的机器分批次. 一次十台机器,如果分3次并行部署,则第一次123,第二次456,第三次789,第四次10. #coding=utf-8 a=[1,2,3,4,5,6 ...

  4. android 入门-防微信拍摄视频 按钮事件处理

    package com.cc.view; import com.cc.R; import com.cc.R.layout; import com.cc.R.menu; import android.o ...

  5. HTTP访问的两种方式(HttpClient+HttpURLConnection)整合汇总对比(转)

    在Android上http 操作类有两种,分别是HttpClient和HttpURLConnection,其中两个类的详细介绍可以问度娘. HttpClient: HttpClient是Apache ...

  6. JS自定义属性兼容

    var obj={}; if(obj.dataset){ obj.dataset.original="11"; }else{ obj.getAttribute("data ...

  7. 【hibernate criteria】hibernate中criteria的完整用法 转

    ---恢复内容开始--- 转自:http://www.360doc.com/content/090313/10/26262_2794855.html 1.Criteria Hibernate 设计了 ...

  8. Android源码学习之模板方法模式应用

    一.模板方法模式定义 模板方法模式定义: defines the skeleton of an algorithm in a method, deferring some steps to subcl ...

  9. loadrunner取出字符串的后面几位

    Action() {    char *phonenum;    int k=1;    phonenum=lr_eval_string("{phoneNum}");//参数化获取 ...

  10. 【JavaScript】变量定义提升、this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级

    参考资料http://caibaojian.com/toutiao/5446 1.所有变量声明(var)或者声明函数都会被提升到当前函数顶部 关于函数表达式,js会将代码拆分为两行代码分别执行.这里需 ...