Evaluate Reverse Polish Notation (STRING-TYPE CONVERTION)
Question
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
1ST TRY
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int operant1;
int operant2;
string str;
int ret;
bool firstOp = false;
for(int i = ; i < tokens.size(); i++)
{
str = tokens[i];
if(!firstOp)
{
operant1 = getInt(str);;
firstOp = true;
}
else if(str == "+")
{
ret = operant1 + operant2;
}
else if(str == "-")
{
ret = operant1 - operant2;
}
else if(str == "*")
{
ret = operant1 * operant2;
}
else if(str == "/")
{
ret = operant1 / operant2;
}
else
{
operant2 = getInt(str);
firstOp = false;
}
}
return ret;
}
int getInt(string str)
{
int ret = ;
for(int i = ; i < str.length(); i++)
{
ret = ret* + str[i];
}
return ret;
}
};
2ND TRY
考虑只有一个操作数的情况
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int operant1;
int operant2;
string str;
int ret;
bool firstOp = false;
for(int i = ; i < tokens.size(); i++)
{
str = tokens[i];
if(!firstOp)
{
operant1 = getInt(str);
firstOp = true;
}
else if(str == "+")
{
operant1 += operant2;
}
else if(str == "-")
{
operant1 -= operant2;
}
else if(str == "*")
{
operant1 *= operant2;
}
else if(str == "/")
{
operant1 /= operant2;
}
else
{
operant2 = getInt(str);
}
}
return operant1;
}
int getInt(string str)
{
int ret = ;
for(int i = ; i < str.length(); i++)
{
ret = ret* + (str[i]-'');
}
return ret;
}
};
Result: Wrong
Input: ["3","-4","+"]
Output: -23
Expected: -1
3RD TRY
考虑负数的情况
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int operant1;
int operant2;
int ret;
string str;
stack<int> operantStack;
for(int i = ; i < tokens.size(); i++)
{
str = tokens[i];
if(str == "+")
{
operant2 = operantStack.top();
operantStack.pop();
operant1 = operantStack.top();
operantStack.pop();
operantStack.push(operant1 + operant2);
}
else if(str == "-")
{
operant2 = operantStack.top();
operantStack.pop();
operant1 = operantStack.top();
operantStack.pop();
operantStack.push(operant1 - operant2);
}
else if(str == "*")
{
operant2 = operantStack.top();
operantStack.pop();
operant1 = operantStack.top();
operantStack.pop();
operantStack.push(operant1 * operant2);
}
else if(str == "/")
{
operant2 = operantStack.top();
operantStack.pop();
operant1 = operantStack.top();
operantStack.pop();
operantStack.push(operant1 / operant2);
}
else
{
operantStack.push(getInt(str));
}
}
return operantStack.top();
}
int getInt(string str)
{
int ret = ;
bool negFlag = false;
for(int i = ; i < str.length(); i++)
{
if(str[i]=='-') negFlag = true;
else if(negFlag)
{
ret = ret* - (str[i]-'');
}
else
{
ret = ret* + (str[i]-'');
}
}
return ret;
}
};
Result: Accepted
4TH TRY
使用内置函数atoi将string转换成int
class Solution {
public:
int evalRPN(vector< string > &tokens) {
stack< int > operandStack;
int operand1;
int operand2;
for(int i = ; i < tokens.size(); i++){
if(tokens[i]=="+"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 += operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="-"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 -= operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="*"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 *= operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="/"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 /= operand1;
operandStack.push(operand2);
}
else{
operand1 = atoi(tokens[i].c_str());
operandStack.push(operand1);
}
}
return operandStack.top();
}
};
Result: Accepted
Evaluate Reverse Polish Notation (STRING-TYPE CONVERTION)的更多相关文章
- 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】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【LeetCode】150. 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 Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- Kali 局域网 DNS 劫持
<一> 所需工具 1: Kali-linux-2017 2: ettercap 0.8.2 3: web 服务器, 这里以 node 为例 <二> 原理 1: DNS劫持 ...
- 【ASP.NET 插件】分享一款富文本web编辑器UEditor
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... <%@ Page Language ...
- Nginx 服务器搭建
什么是Nginx ? Nginx与Apache IIS等软件一样,是一款服务器软件,为web站点提供服务 除此之外,Nginx 还是一款反向代理服务器,我们可以利用Nginx实现负载均衡 所谓负载均衡 ...
- 功能测试-UI测试思考点
界面是否美观 元素大小 界面元素是否对齐方式统一 界面字体属性是否正确 界面链接及触发动作( 链接的地址是否正确,不允许存在死链的情况 链接打开方式,当前页面还是新开页面 鼠标点击后的颜色是否美观,不 ...
- JeeWx捷微3.1小程序版本发布,支持微信公众号,微信企业号,支付窗——JAVA版开源微信管家
支持小程序,JeeWx捷微3.1小程序版本发布^_^ JeeWx捷微V3.1——多触点小程序版本管理平台(支持微信公众号,微信企业号,支付窗) JeeWx捷微V3.1.0版本紧跟微信小程序更新,在 ...
- 高德地图 API 计算两个城市之间的距离
1. 目前在项目中,遇到一个需求不会做,就是要计算两个城市之间的距离,而这两个城市的输入是可变的,如果要使用数据库来先存储两地之间的距离,调用的时候再来调用,那么存数据的时候,要哭的,因为光是省级区域 ...
- C++17尝鲜:结构化绑定声明(Structured Binding Declaration)
结构化绑定声明 结构化绑定声明,是指在一次声明中同时引入多个变量,同时绑定初始化表达式的各个子对象的语法形式. 结构化绑定声明使用auto来声明多个变量,所有变量都必须用中括号括起来. cv-auto ...
- 配置nginx实现windows/iis应用负载均衡(转载)
配置nginx实现windows/iis应用负载均衡 nginx是俄罗斯人开发的一款跨平台的高性能HTTP和反向代理服务器,可以利用它实现web应用服务器的负载均衡. 反向代理是指将用户请求通过代 ...
- JAVA 16进制转ASCII -- 2018年5月25日 周五
/** * 16进制转ASCII * * @param hex * @return */ public static String hex2Str(String hex) { StringBuilde ...
- Android Studio 版本间区别
2.3.2 ->3.0.1 Gradle版本为4.1 com.android.tools.build:gradle:3.0.x Android Monitor 被换成了 Android P ...