【刷题-LeetCode】150 Evaluate Reverse Polish Notation
- 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 : 影响代码速度
- for循环中直接枚举会慢一些
- 函数传参时,传值会慢一些,尽量使用传引用
【刷题-LeetCode】150 Evaluate Reverse Polish Notation的更多相关文章
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
随机推荐
- JAVA判断是否是微信内置浏览器,是否是在微信内打开
/** * 通过请求头判断是否是微信内置浏览器,是否是在微信内打开 * @param request * @return */ @RequestMapping(value = "/hello ...
- JAVA中JDK1.8的LocalDateTime日期类的操作方法
LocalDateTime与Date相互转换参考:https://www.cnblogs.com/pxblog/p/13745972.html 关键类 Instant:瞬时时间. LocalDate: ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】478. Generate Random Point in a Circle 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/generate ...
- The Balance(poj2142)
The Balance Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5452 Accepted: 2380 Descr ...
- eclipse的安装及最大子数组求和
我安装的是eclipse.由于eclipse是一个基于Java的课扩展开发平台,所以在安装eclipse之前要先安装Java的开发工具JDK(Java Devolopment Dit),且安装JDK需 ...
- 【Java笔记】Java使用mysql包注意
注意 安装的mysql5.x版本对应 5.x版本的驱动包 安装的mysql8.x版本对应 8.x版本的驱动包 如果安装的MySQL版本和驱动包版本不符合,则Java的连接不了数据库
- Linux开关机与登录注销
开机和重启 shutdown -h now:立即关机计算机 shutdown -h 1:1分钟后关机 halt:立即关机 reboot:立即重启 sync:把内存的数据同步到磁盘 注: shutdow ...
- 【】Apache Ranger剖析:Hadoop生态圈的安全管家
前言 2016年,Hadoop迎来了自己十周岁生日.过去的十年,Hadoop雄霸武林盟主之位,号令天下,引领大数据技术生态不断发展壮大,一时间百家争鸣,百花齐放.然而,兄弟多了不好管,为了抢占企业级市 ...
- shell3-循环
常用的循环语句有3种: <1>for <2>while <3>utile 1.for语句的格式: for 变量名 in 列表: do 循环体 done 如何生成列表 ...