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 +, -, ...
随机推荐
- elasticsearch template
# curl -XPUT localhost:9200/_template/template_1 -d '{"template" : "te*","s ...
- 大数据入门到精通13--为后续和MySQL数据库准备
We will be using the sakila database extensively inside the rest of the course and it would be great ...
- compatible
compatible 英 [kəmˈpætəbl] 美 [kəmˈpætəbəl] adj. 兼容的,相容的; 和谐的,协调的; [生物学] 亲和的; 可以并存的,能共处的;
- 结构体指offsetof宏详细解析
1.#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER) (include/linux/stddef.h) ...
- TZOJ 1693 Silver Cow Party(最短路+思维)
描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big ...
- Solidity的三种转账方式与比较
转账的3种方式 123 address.transfer()address.send()address.call.value().gas()() 转账transfer 12345678910 func ...
- 【转】最近用Timer踩了一个坑,分享一下避免别人继续踩
[转]最近用Timer踩了一个坑,分享一下避免别人继续踩 最近做一个小项目,项目中有一个定时服务,需要向对方定时发送数据,时间间隔是1.5s,然后就想到了用C#的Timer类,我们知道Timer 确实 ...
- Zookeeper Client基础操作和Java调用
## Zookeeper > Zookeeper目前用来做数据同步,再各个服务之前同步关键信息 i.客户端操作 1. 创建 create [-s] [-e] path data acl -s 为 ...
- python基本数据类型之字符串(二)
python基本数据类型之字符串(二) 替换方法 python中字符串的替换方法主要有:center.rjust\ljust.expandtabs.format\format_map(格式化).str ...
- spring+shiro+ehcache整合
1.导入jar包(pom.xml文件) <!-- ehcache缓存框架 --> <dependency> <groupId>net.sf.ehcache</ ...