leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation
Total Accepted: 24595 Total Submissions: 123794My Submissions
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
思路:栈的简单应用
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <cstdlib> using namespace std; class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> s;
string str;
int a, b; for (int i = ; i < tokens.size(); i++) {
str = tokens[i];
if (str == "+" || str == "-" || str == "*" ||
a = s.top(); s.pop();
b = s.top(); s.pop();
switch(str[]) {
case '+': s.push(b+a); break;
case '-': s.push(b-a); break;
case '*': s.push(b*a); break;
case '/': s.push(b/a); break;
}
}
else {
s.push(atoi(str.c_str()));
}
}
a = s.top();s.pop();
return a;
}
}; int main(int argc, char *argv[]) {
Solution* solution = new Solution();
vector<string> tokens;
tokens.push_back("");
tokens.push_back("");
tokens.push_back("+");
tokens.push_back("");
tokens.push_back("*"); cout << solution->evalRPN(tokens) << endl; vector<string> tokens2;
tokens2.push_back("");
tokens2.push_back("");
tokens2.push_back("");
tokens2.push_back("/");
tokens2.push_back("+");
cout << solution->evalRPN(tokens2) << endl;
return ;
}
leetcode - [2]Evaluate Reverse Polish Notation的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
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
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
随机推荐
- centos7下swoole1.9的安装与HttpServer的使用
一.下载swoole源码包 https://github.com/swoole/swoole-src/releases 如:swoole-src-1.9.6.tar.gz 二.编译安装 > yu ...
- vmware磁盘空间扩展
往vmware虚拟机中导入数据库或者文件以后经常出现磁盘空间不够用.这个时候就需要扩展一下磁盘的大小. 笔者本来60G,现在想扩展到100G 命令如下 D:\Program Files (x86)\V ...
- Android Studio 使用入门
Android Studio 快捷键 Action Mac OSX Win/Linux 注释代码(//) Cmd + / Ctrl + / 注释代码(/**/) Cmd + Option + / Ct ...
- ubuntu 无法挂载U盘
问题描述: usb 1-1: device descriptor read/64,error 18usb 1-1: device descriptor read/64,error 18usb 1-1: ...
- Windows服务安装、卸载、启动和关闭的管理器
最近在重构公司的系统,把一些需要独立执行.并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了.但是每次都要建立Windows服务项目 ...
- c10k C10M
高性能网络编程(二):上一个10年,著名的C10K并发连接问题 阅读(22369) | 评论(9)收藏10 淘帖1 赞4 JackJiang Lv.9 1 年前 | |只看大图 1. ...
- this 关键字 详解
JS中的this关键字让很多新老JS开发人员都感到困惑.这篇文章将对this关键字进行完整地阐述.读完本文以后,您的困惑将全部消除.您将学会如何在各种不同的情形正确运用this. 我们和在英语.法语这 ...
- Python爬虫的原理
简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现我们眼前: 一.爬虫是什么? ...
- UIDataPicker 时间选择器
自用时间选择器 @interface ViewController () { UILabel *cityLabel; UIDatePicker *datePicker; } //@property(n ...
- Nowcoder 练习赛26E 树上路径 - 树剖
Description 传送门 给出一个n个点的树,1号节点为根节点,每个点有一个权值 你需要支持以下操作 1.将以u为根的子树内节点(包括u)的权值加val 2.将(u, v)路径上的节点权值加va ...