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 ...
随机推荐
- 什么是ThreadLocal
当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本.ThreadLocal ...
- WPF 自定义鼠标光标
在程序中使用自定义鼠标光标的三种方式: RadioButton senderButton = sender as RadioButton; 方式一: str ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 1: ordinal not in range(128)
待研究: compressed_data = zlib.compress(json.dumps(data), 9) file_data = MySQLdb.escape_string(compress ...
- UNITY 的GC ALLOC到底是什么
U3D的Profiler中的GC ALLOC 项让人很麻烦,一直搞不清楚它是什么,因为 GC 是垃圾回收,而alloc是内存分配,那么 GC ALLOC 是 垃圾回收内存分配? 这个名字起的太TM烂了 ...
- Zabbix实现自动发现端口并监控
1.新建客户端需要的脚本 # vim discovertcpport.sh #!/bin/bash portarray=(`sudo netstat -tnlp|egrep -i "$1&q ...
- select min from 连接
预先准备 create table p( name ) ); insert into p values('黄伟福'); insert into p values('赵洪'); insert into ...
- form表单 获取与赋值
form表单中使用频繁的组件: 文本框.单选框.多选框.下拉框.文本域form通过getValues()获取表单中所有name的值 通过setValues({key:values})给对应的name值 ...
- Delphi 透明窗体显示文字
设置窗体属性:BorderStyle 属性设置为 bsNoneColor 属性设置为 clWhite(白色:窗体背景色)TransparentColor 属性设置为 trueTransparentCo ...
- mybatis实现一对多连接查询
问题:两个对象User和Score,它们之间的关系为一对多. 底层数据库为postgresql,ORM框架为mybatis. 关键代码如下: mybatis配置文件如下: mybatis.xml文件内 ...
- 无法加载ISAPI 筛选器 当前配置只支持加载为 AMD64 处理器体系结构创建的映像
无法加载ISAPI 筛选器 当前配置只支持加载为 AMD64 处理器体系结构创建的映像 2011-11-9 0:18:49来源:本站原创作者:清晨320我要评论(0) 今天服务器的伪静态死活加载不上去 ...