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

C++

class Solution {
public:
int evalRPN(vector<string> &tokens){
int len = tokens.size();
stack<int> S;
for (int i = 0; i< len; i++){
if ("+" == tokens[i] || "-" == tokens[i] || tokens[i] == "*" || tokens[i] == "/"){
int arg2 = S.top(); S.pop();
int arg1 = S.top(); S.pop();
S.push(runOperator(arg1,arg2,tokens[i][0]));
}else
S.push(stoi(tokens[i]));
}
return S.top();
}
int runOperator(int arg1,int arg2,char optor){
if('+' == optor) return arg1 + arg2;
else if('-' == optor) return arg1 - arg2;
else if('*' == optor) return arg1 * arg2;
else return arg1 / arg2;
}
};

evaluate-reverse-polist-notation leetcode C++的更多相关文章

  1. 150. Evaluate Reverse Polish Notation - LeetCode

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

  2. Evaluate Reverse Polish Notation——LeetCode

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

  3. Evaluate Reverse Polish Notation leetcode java

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

  4. Evaluate Reverse Polish Notation --leetcode

    原题链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目大意:给出逆波兰式,然后求其结果. 解题方法:单个栈 ...

  5. 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 ...

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

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

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

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

  8. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  9. 【LeetCode练习题】Evaluate Reverse Polish Notation

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

  10. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

随机推荐

  1. Django学习day07随堂笔记

    今日考题 """ 今日考题 1.必知必会N条都有哪些,每个都是干啥使的 2.简述神奇的双下划线查询都有哪些方法,作用是什么 3.针对多对多外键字段的增删改查方法有哪些,各 ...

  2. Mysql实现排序

    排序   SELECT     obj.user_id,obj.score,@rownum := @rownum + 1 AS rownum FROM     (         SELECT     ...

  3. tomcat URI get 参数中文传到后台 乱码 URIEncoding

    * 修改tomcat server.xml 找到这一行 <Connector connectionTimeout="20000" port="80" pr ...

  4. Selenium多浏览器并行测试

    如果需要同时在IE.firefox.chrome进行测试,可以使用grid. Selenium Grid是一个智能代理服务器,允许Selenium测试将命令路由到远程Web浏览器实例.其目的是提供一种 ...

  5. modern php closure 闭包

    * 在array_map()函数中使用闭包 <?php $numbersPlusOne = array_map(function($number) { return $number + 1; } ...

  6. 一文让你快速入门pytest框架

    pytest是什么 官方文档描述: pytest is a framework that makes building simple and scalable tests easy. Tests ar ...

  7. mybatis关系表

    <select id="selectSingleQuestion" resultType="remarkPaper"> select FrontTi ...

  8. WPF进阶技巧和实战03-控件(5-列表、树、网格04)

    ListView控件 ListView继承自简单的没有特色的ListBox,增加了对基于列显示的支持,并增加了快速切换视图或显示模式的能力,而不需要重新绑定数据以重新构建列表. ListView类继承 ...

  9. 4-让线程睡眠的sleep方法

    让线程睡眠的sleep方法 Thread类有一个静态的sleep方法,当一个执行中的线程调用了Thread的sleep方法,调用线程就会让出指定时间的执行权,也就是在这期间不参与CPU调度,但是该线程 ...

  10. 10.13 Nginx 负载均衡

    七层负载均衡server { listen 80; server_name localhost; location / { proxy_pass http://name; //调用集群 } } ups ...