1. Evaluate Reverse Polish Notation

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

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

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

逆波兰表达式中,两个操作数紧跟一个运算符

class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int>s1;
for(int i = 0; i < tokens.size(); ++i){
if(tokens[i].size() == 1 && !isdigit(tokens[i][0])){
int x1 = s1.top();
s1.pop();
int x2 = s1.top();
s1.pop();
s1.push(compute(x2, x1, tokens[i]));
}else{
s1.push(stoi(tokens[i]));
}
}
return s1.top();
}
int compute(int &x1, int &x2, string &op){
if(op == "+"){
return x1 + x2;
}else if(op == "-"){
return x1 - x2;
}else if(op == "*"){
return x1 * x2;
}else if(op == "/"){
return x1 / x2;
}
return -1;
}
};

Note : 影响代码速度

  1. for循环中直接枚举会慢一些
  2. 函数传参时,传值会慢一些,尽量使用传引用

【刷题-LeetCode】150 Evaluate Reverse Polish Notation的更多相关文章

  1. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

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

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

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

  3. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

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

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

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

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

  6. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  7. LeetCode——150. Evaluate Reverse Polish Notation

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

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

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

  9. 150. Evaluate Reverse Polish Notation - LeetCode

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

随机推荐

  1. linux下记录入站请求

    将内网机器通过frp映射到公网后,内网主机受到大量ssh爆破攻击,攻击来源为frp的服务端,仅在内网机器上无法追踪到真实的攻击来源的ip.下面记录了在frp服务端监控指定端口的入站数据,找到真正的攻击 ...

  2. wget 用法http://www.cnblogs.com/cy-8593/p/9523035.html

    http://www.cnblogs.com/cy-8593/p/9523035.html

  3. word文档打钩记录快捷键

    先按住键盘上的 Alt 键不放,然后在小键盘区(数字键区)输入 9745 ,最后松开 Alt键.

  4. mysql添加上log_bin步骤如下

    mysql添加上log_bin步骤如下

  5. Dockerfile使用OracleJDK创建自定义tomcat8镜像

    我们默认下载的tomcat镜像是用的openjdk ,但是我们有些项目必须使用oraclejdk 那就不能使用官方的tomcat镜像,只能重新自定义一个镜像 Dockerfile文件 FROM cen ...

  6. 【九度OJ】题目1018:统计同成绩学生人数 解题报告

    [九度OJ]题目1018:统计同成绩学生人数 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1018 题目描述: ...

  7. 【九度OJ】题目1185:特殊排序 解题报告

    [九度OJ]题目1185:特殊排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1185 题目描述: 输入一系 ...

  8. Go语言核心36讲(新年彩蛋)--学习笔记

    新年彩蛋 | 完整版思考题答案 基础概念篇 Go 语言在多个工作区中查找依赖包的时候是以怎样的顺序进行的? 答:你设置的环境变量GOPATH的值决定了这个顺序.如果你在GOPATH中设置了多个工作区, ...

  9. rapidjson解析与构造实例

    void rapidjson1(){ rapidjson::StringBuffer s; rapidjson::Writer<rapidjson::StringBuffer> write ...

  10. 【Linux】Linux安装JDK

    1.下载linux版本的JDK1.8 链接:JDK下载链接 提取码:fxn4 链接:Xshell下载链接 提取码:439l 2.使用Xshell连接虚拟机,上传文件 在 /usr目录下创建一个新文件夹 ...