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 ...
随机推荐
- DOS 和 DDOS 攻击
<一>: ICMP 协议 01: 是什么 => 互联网信心控制协议. Internet Control Message Protocol 02: 干什么 => 用于实现链路连通 ...
- Image Base64 Datasnap Image delphi与c#互相兼容识别
delphi用,不能与java.c#互相识别. procedure TServerMethods.UpdateDoc(ItemID : integer; doc : TStream); delphi用 ...
- 24_ajax请求_使用axios
前置说明: 1.React本身只关注页面,并不包含发送ajax请求的代码 2.前端应用需要通过ajax请求与后台进行交互(json数据) 3.React应用中需要集成第三方ajax库(或自己进行封装) ...
- OC代码编译成c++代码 编译器命令
xcrun -sdk iphoneos clang -arch x86_64 -rewrite-objc Person+Test.m clang -rewrite-objc -fobjc-arc -s ...
- git使用 从远程库克隆和更新到本地
从远程库克隆到本地. git clone git@github.com:kingbook/Framework.git 或 git clone http://github.com/kingBook/Fr ...
- NIPS 2016上22篇论文的实现汇集
http://blog.csdn.net/jiandanjinxin/article/details/54087592 日前,LightOn CEO 兼联合创始人 Igor Carron 在其博客上放 ...
- LeetCode OJ 49. Group Anagrams
题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...
- 如何创建Servlet
//Servlet的生命周期:从Servlet被创建到Servlet被销毁的过程 //一次创建,到处服务 //一个Servlet只会有一个对象,服务所有的请求 /* * 1.实例化(使用构造方法创建对 ...
- JAVA面试中的陷阱
第一,谈谈final, finally, finalize的区别.最常被问到. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以impl ...
- thread == 售票
import org.apache.xerces.util.SymbolTable; public class ThreadDemo1 { public static void main(String ...