LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
题目: Evaluate Reverse Polish Notation
Evaluatethe value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["", "", "+", "", "*"] -> (( + ) * ) ->
["", "", "", "/", "+"] -> ( + ( / )) ->
简单题,借助一个stack就可以实现,将数值入栈,遇操作符时将两个数值出栈,计算后再入栈,如此即可实现。
int evalRPN(vector<string> &tokens)
{
stack<int> stokens;
for (int i = ; i < tokens.size(); ++ i) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int x1 = stokens.top();
stokens.pop();
int x2 = stokens.top();
stokens.pop();
int x3 = caculate(x1, x2, tokens[i]); //计算之后再入栈
stokens.push(x3);
}
else {
stokens.push(atoi(tokens[i].c_str()));
}
}
return stokens.top();
}
int caculate(int x1, int x2, string s)
{
int result;
if (s == "+")
result = x1 + x2;
else if (s == "-")
result = x1 - x2;
else if (s == "*")
result = x1 * x2;
else {
if (x1 >= x2)
result = x1 / x2;
else
result = x2 / x1;
}
return result;
}
LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium的更多相关文章
- [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 +, -, ...
- LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode150_Evaluate Reverse Polish Notation评估逆波兰表达式(栈相关问题)
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+, ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- [leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...
- [LeetCode] Evaluate Reverse Polish Notation [2]
题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- Flask最强攻略 - 跟DragonFire学Flask - 第三篇 Flask 中的 request 之 先知道有这么个东西
每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的 为了了解Flask的request中都有什么东西,首先我们要写一个前后端的交互 基于HTML + Flask 写一 ...
- 获取MessageBox按钮本地字符串(OK、Cancel、Yes、No等)
问题仍然由定制MessageBox引发. 定制MessageBox,虽加入自定义些东西,但仍然希望,最大限度接近系统原生框.碰到的问题,就是其钮文本. 即如MessageBox.Show()之Mess ...
- hdpi对应分辨率
ldpi QVGA (240×320) mdpi HVGA (320×480) hdpi WVGA (480×800),FWVGA (480×854) xhdpi 720P(1280*720) ...
- Py西游攻关之RabbitMQ、Memcache、Redis
Py西游攻关之RabbitMQ.Memcache.Redis RabbitMQ 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议 ...
- CentOS7 开启网卡,设置开机启用网卡
默认centos和redhat7都是不启用有线网卡的,要么手动开启,要么安装时直接启用(安装时启用网卡和指定IP最省事)! 一 .临时启用网卡,关机或重启后,网络不会自动开启1.使用命令 ip add ...
- class空格多类名
1.class属性唯一但是有空格,选择空格两边唯一的哪一个 <div id="tempConfigTable" class="dtb-style-1 table-d ...
- 过滤器(Filter)与拦截器(Interceptor)区别
过滤器(Filter)与拦截器(Interceptor)区别 过滤器(Filter) Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途 ...
- HDU 6214.Smallest Minimum Cut 最少边数最小割
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- python 数据可视化(matplotlib)
matpotlib 官网 :https://matplotlib.org/index.html matplotlib 可视化示例:https://matplotlib.org/gallery/inde ...
- Python之路(第二十八篇) 面向对象进阶:类的装饰器、元类
一.类的装饰器 类作为一个对象,也可以被装饰. 例子 def wrap(obj): print("装饰器-----") obj.x = 1 obj.y = 3 obj.z = 5 ...