[LeetCode] Evaluate Reverse Polish Notation stack 栈
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
题目是用 stack 维护一个公式的进出,判断方法还可以,开始忘记数可能为负,后面改进了。
#include <stack>
#include <iostream>
#include <string>
#include <vector>
using namespace std; class Solution {
public:
int evalRPN(vector<string> &tokens) {
int n = tokens.size();
stack<int > tmp;
for(int i=;i<n;i++){
if(tokens[i][]>=''&&tokens[i][]<=''){
tmp.push( helpFun(tokens[i]) );
continue;
}
if(tokens[i][]=='-'&&tokens[i][]!='\0'){
tmp.push( helpFun( tokens[i]));
continue;
}
int rgt = tmp.top();
tmp.pop();
int lft = tmp.top();
tmp.pop();
switch (tokens[i][]){
case '+':
tmp.push( lft + rgt );
break;
case '-':
tmp.push(lft - rgt);
break;
case '*':
tmp.push(lft * rgt);
break;
case '/':
tmp.push(lft / rgt);
break;
}
}
return tmp.top();
} int helpFun(string str)
{
int sum = ,i = ;
if (str[]=='-')
i = ;
for(;i<str.length();i++)
sum = sum*+str[i]-'';
return str[]=='-'?-sum:sum;
}
}; int main()
{
vector<string> tokens{"","-4","+"};
Solution sol;
cout<<sol.evalRPN(tokens)<<endl;
// for(int i=0;i<tokens.size();i++)
// cout<<tokens[i]<<endl;
return ;
}
[LeetCode] Evaluate Reverse Polish Notation stack 栈的更多相关文章
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LeetCode] 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 +, -, ...
- leetcode——Evaluate Reverse Polish Notation 求算式值(AC)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation (Stack)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution
#define ADDITION '+' #define SUBSTRACTION '-' #define MULTIPLICATION '*' #define DIVISION '/' class ...
随机推荐
- Java设置模式
单例模式 装饰者模式 代理模式
- 您的手机上未安装应用程序 android 点击快捷方式提示未安装程序的解决
最近APP出现一个很奇怪的问题,在Android 4.4.2和android 4.4.3系统上点击应用的快捷方式,打不开应用,而且会提示未安装程序. 确认了应用的MainActivity中设置了and ...
- Python基础——安装运行
Python是如何运行的? 像绝大多数编程语言一样,要在计算机上能够运行python程序,至少需要安装一个最小的Python包:一个Python解释器和支持的库. 安装Python 安装包下载:htt ...
- CenOS 配置C/C++语言
1.下载eclipse+CDT组合包. 2.电脑上安装GCC, G++ 3.在eclipse上创建一个C++ project 4. Eclipse CDT功能很强大,安装完虽然可以编译运行c++程序, ...
- Android学习记录(7)—Intent中显示意图和隐式意图的用法
Intent(意图)主要是解决Android应用的各项组件之间的通讯. Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的 ...
- 《数据结构》C++代码 栈与队列
线性表中,先进先出的叫队列,先进后出的叫栈.队列常用于BFS,而在函数递归层数过高时,需要手动实现递归过程,这时候便需要写一个“手动栈”. 有时候,我们会有大量数据频繁出入队列,但同时存在其内的元素却 ...
- 【Adaptive Boosting】林轩田机器学习技法
首先用一个形象的例子来说明AdaBoost的过程: 1. 每次产生一个弱的分类器,把本轮错的样本增加权重丢入下一轮 2. 下一轮对上一轮分错的样本再加重学习,获得另一个弱分类器 经过T轮之后,学得了T ...
- script通过script标签跨域加载数据
/********************************************************** 说明:跨域请求数据Javascript组件 ------------------ ...
- Python 列表、元组、字典及集合操作详解
一.列表 列表是Python中最基本的数据结构,是最常用的Python数据类型,列表的数据项不需要具有相同的类型 列表是一种有序的集合,可以随时添加和删除其中的元素 列表的索引从0开始 1.创建列表 ...
- 孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录
孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数 ...