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. MVC基于角色权限控制--管理角色

    管理角色分为 添加角色.删除角色.修改角色.给角色分配权限(修改角色权限) 新建RoleInfoController继承BaseController namespace CZBK.ItcastOA.W ...

  2. vbox 按照增强工具 centos7

    命令:mount -t auto /dev/cdrom /mnt/cdrom 这命令就是把CentOS CDROM挂载在/mnt/cdrom目录中,这样我们就可以访问光盘里面的内容了.执行“mount ...

  3. 腾讯云Linux VPS新硬盘分区与挂载教程(面板重装不丢失数据)

    以腾讯云Centos系统服务器为例,小记的是数据盘不在本地,大小为20G,以下的教程来自小夕博客的一篇相关添加教程的修改,适合腾讯云Linux Centos系统.说明:参数也许不对,我没有截图了,但所 ...

  4. nginx 和 php超时设置

    nginx.conf ---  http节: keepalive_timeout 600; #客户端浏览器超时时间fastcgi_connect_timeout 600; #php-fpm连接超时时间 ...

  5. LeetCode 题解:Populating Next Right Pointers in Each Node I & II 二有难度。考虑不全面。

    每次应该把root同层的右侧节点传过来.如果没有,就传NULL. 同时,应该是先右后左. 感觉这次的代码还挺简洁的.. void construct(struct TreeLinkNode *root ...

  6. ubuntu 使用命令行登录oracle

    1.检查环境变量设置 echo $ORACLE_HOME 2.配置oracle数据库信息,将oracle地址端口等信息放在$ORACLE_HOME/network/admin目录下的tnsnames. ...

  7. cxgrid中回车键光标移到下列

    OptionsBehavior.GoToNextCellOnEnter:=True; 更完善的回车 可以在焦点到了最后一列再回车时有下一行则移到下一行的第一列,没有下一行则新增记录并移到第一列 pro ...

  8. 学习笔记2:postman 的基本使用

    1.get 请求 请求接口:http://xx.xx.xx.xx/api/user/stu_info 请求参数:stu_name 请求参数是放在请求 URL 里的,点击 Params添加参数,key ...

  9. Array.Resize(ref arry, size);

    数组原来的内容不变,后面添加新的空间. 内部操作应该是:重新分配了一块空间,然后将旧的内容拷过去

  10. oracle第三天笔记

    DDL语句管理表 /* Oracle体系结构: 数据库 ---> 数据库实例ORCL ---> 表空间 (用户里面的创建表) ---> 数据文件 地球 ---> 中国 ---& ...