【Evaluate Reverse Polish Notation】cpp
题目:
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
代码:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> sta;
for ( size_t i = ; i < tokens.size(); ++i )
{
if ( tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" || tokens[i]=="/" )
{
int right = sta.top();
sta.pop();
int left = sta.top();
sta.pop();
if ( tokens[i]=="+" ) { sta.push(left+right); continue; }
if ( tokens[i]=="-") { sta.push(left-right); continue; }
if ( tokens[i]=="*") { sta.push(left*right); continue; }
if ( tokens[i]=="/") { sta.push(left/right); continue; }
}
else
{
sta.push(atoi(tokens[i].c_str()));
}
}
return sta.top();
}
};
tips:
堆栈求逆波兰表达式口诀:遇上数字进栈;遇上操作符先出栈两个元素,计算结果后再压入栈。
======================================================
第二次过这道题,思路记得比较清楚。这里需要记住一个函数c++ atoi (string 转 int),这样在读入的时候转一次就够了。
stack里面存的是数字。操作符不进栈。
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> sta;
for ( int i=; i<tokens.size(); ++i )
{
if ( tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" || tokens[i]=="/" )
{
int right = sta.top(); sta.pop();
int left = sta.top(); sta.pop();
if ( tokens[i]=="+" )
{
sta.push(right+left);
}
else if ( tokens[i]=="-")
{
sta.push(left - right);
}
else if ( tokens[i]=="*" )
{
sta.push(right * left);
}
else
{
sta.push(left / right);
}
}
else
{
sta.push(atoi(tokens[i].c_str()));
}
}
return sta.top();
}
};
【Evaluate Reverse Polish Notation】cpp的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- iOS中UIKit——UIDataDetectors(数据检测器)它将电话、邮件、网址等变为链接
1.它用于UITextView和UIWebView,属性名为:dataDetetorTypes 2.此属性可以设定使符合电话.邮件.网址.符合格式的日期等文字变为链接文字. 3.电话号码点击后拨出电话 ...
- DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):
一.DataGridView属性设置 1.我们单击选中行的时候,默认是选择一个单元格,不能选择一整行,我们只需设置DataGridView的属性SelectionMode为FullRowSelect ...
- spark性能调优:资源优化
在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以及如何设置 ...
- mac里边配置android开发环境,intellij开发工具:
1 在android的官网下载 android sdk的mac版 http://developer.android.com/sdk/index.html 选择mac的版本 下载后打开sdk-mana ...
- asp.net实现md5加密方法详解
MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文. 例如:明文为:abcdefg 通过一些列运算 得到 密文 7ac66c0f148de9519b8bd264312c4d64 它具有两个特 ...
- Delphi For Android 开发笔记 1 - 开发工具介绍
在开始前,推荐喜欢delphi或者pascal的朋友,如果想将原来Windows的软件工程移植到Android,可使用CodeTyphon+Delphi XE7进行开发. 1.CodeTyphon C ...
- scrapy爬虫框架入门教程
scrapy安装请参考:安装指南. 我们将使用开放目录项目(dmoz)作为抓取的例子. 这篇入门教程将引导你完成如下任务: 创建一个新的Scrapy项目 定义提取的Item 写一个Spider用来爬行 ...
- Linux获取用户主目录
#!/usr/bin/python# -*- coding:utf-8 -*-import sysimport osclass get_home_path(object): def __init__( ...
- Ztack学习笔记(4)-系统网络分析
协调器的组网,终端设备和路由设备发现网络以及加入网络 //第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统 ...
- SQL Server 一些关键字详解(二)
1.LEFT JOIN 容易让人误解的地方 背景:因为在网上搜了下 LEFT JOIN 和 OUTER APPLY 的区别,时发现,有的网友解释为: 1) A left join B 的连接 ...