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)的更多相关文章

  1. 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 ...

  2. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  3. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  4. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  5. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  6. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  8. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  9. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  10. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. Win7查看开关机记录

    通过系统日志可以查看,这里记得的日志很多,需要筛选一下,来个图片看的清楚: 事件ID的12,13就代表开关机,具体信息会在窗口下方显示.

  2. Notepadd ++ PluginManager安装

    下载地址https://github.com/bruderstein/nppPluginManager/releases 解压后有2个包plugins和updater 分别放入C:\Program F ...

  3. linux mce的一些相关内容和用户态监控的设计方法

    之所以想起写一点关于mce的东西,倒不是因为遇到mce的异常了,之前遇到过很多mce的异常,内存居多,但没有好好记录下来,写这个是因为参加2018 clk南京会议的一点想法. void __init ...

  4. 基于Java SE集合的图书管理系统

    图书管理系统一.需求说明1.功能:登录,注册,忘记密码,管理员管理,图书管理.2.管理员管理:管理员的增删改查.3.图书管理:图书的增删改查.4.管理员属性包括:id,姓名,性别,年龄,家庭住址,手机 ...

  5. 维护没有源代码的遗留 Java 项目

    维护没有源代码的遗留 Java 项目 Give Those Sweets Some Love --> 有时你可能不得不修改一些只有 Jar 和 .class 的 Java 项目. 要修改 Jar ...

  6. Windows Server 2012开启磁盘性能计数器

    Windows Server 2012默认情况下已经禁用了磁盘性能计数器,打开任务管理器后,无法像Win8一样在性能选项卡中看到“磁盘”使用情况,可能是因为微软考虑到安装此服务器系统的硬件都会非常好, ...

  7. maven 创建war类型项目

    1. 创建maven project时选择packaging为war 2. 在webapp文件夹下新建META-INF和WEB-INF/web.xml 3. 在pom.xml中添加web项目需要的ja ...

  8. 学JS的心路历程-Promise(三)

    今天我们来说then一些特殊情况以及Promise.all()与Promise.race(). 我们都知道函式作为参数传入时,可以参照的方式传入,也能传入时执行拿回传值作使用: function us ...

  9. UVA11572-Unique Snowflakes-(最长不同连续子序列)

    题意:给n个数,求最长不同连续子序列.n<=1e6. 解题过程: 1.记录数据存于数组 2.用左右指针l和r指向这段连续区间 3.右指针往右走,如果遇到没有存在于set集合的数就插入集合 否则左 ...

  10. laravel 命令行创建controller 创建数据库表迁移 创建module

    1.php artisan 查看命令列表 2.php artisan make:controller ArticleController 命令 创建控制器 3.创建数据库迁移表 创建文章表 php a ...