[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 ...
随机推荐
- PLC状态机编程第三篇-RS信号处理
我们今天简要介绍RS指令在状态机中怎么处理的.有些设备按下停止按钮后,没有马上停止,而是到原点后才停止,那么这种情况在状态机中如何表示呢?我们以案例说明之,下面是我们的控制描述. 控制描述 小车从左位 ...
- Ubuntu 16.04上安装并配置Postfix作为只发送SMTP服务器
如果大家已经在使用第三方邮件服务方案发送并收取邮件,则无需运行自己的邮件服务器.然而,如果大家管理一套云服务器,且其中安装的应用需要发送邮件通知,那么运行一套本地只发送SMTP服务器则更为理想. 如何 ...
- WPF系列教程——(二)使用Prism实现MVVM设计模式 - 简书
原文:WPF系列教程--(二)使用Prism实现MVVM设计模式 - 简书 本文假设你已经知道MVVM设计模式是什么,所以直接进入正题,今天我们就用Prism来实现WPF的MVVM设计模式,百度上关于 ...
- P2370 yyy2015c01的U盘
P2370 yyy2015c01的U盘 题目背景 在2020年的某一天,我们的yyy2015c01买了个高端U盘. 题目描述 你找yyy2015c01借到了这个高端的U盘,拷贝一些重要资料,但是你发现 ...
- hadoop中namenode发生故障的处理方法
Namenode 故障后,可以采用如下两种方法恢复数据: 方法一:将 SecondaryNameNode 中数据拷贝到 namenode 存储数据的目录: 方法 二: 使用 -importCheckp ...
- erlang节点局域网通信
节点1: F:\WorkSpace\Server\src>erl -name hw@192.168.10.142 -setcookie 4213 consulting .erlang in &q ...
- 在iis上部署asp.net mvc2.0
mvc2.0是vs2010自带的,在开发环境下可以直接部署在iis中.在生产环境下,如果不能找到正确的mvc2.0版本,可以直接把开发环境下的System.Web.Mvc.dll拷贝过去使用. 1, ...
- nosetests
1.nosetests 执行出测试报告 提前安装 插件nose-html系列插件 nosetests -v --with-html-output --html-out-file=报告名.html ...
- 剑指offer-从尾到头打印链表03
class Solution: # 返回从尾部到头部的列表值序列,例如[1,2,3] def printListFromTailToHead(self, listNode): # write code ...
- android系统联系人分组特效实现(1)---分组导航和挤压动画
1.打开activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr ...