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 逆波兰式,不用说,肯定考虑栈。

主要是题目所给的是字符串的数组,需要多次进行数字到字符串或者字符串到数字的转换,具体实现参考我的blog,整数转字符串,字符串转整数

这里我采用的是c++标准库sstream来实现转换。

代码:

class Solution {
private:
bool isSymbol(string a){
return a=="+"||a=="-"||a=="*"||a=="/";
}
int Evaluate(int a,int b,char c){
switch (c)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a/b;
break;
default:
break;
}
}
public:
int evalRPN(vector<string> &tokens) {
stack<string> container;
for(int i=;i<tokens.size();++i){
if(isSymbol(tokens[i])&&!container.empty()){
string temp2Str=container.top();container.pop();
string temp1Str=container.top();container.pop();
int temp2;
int temp1;
stringstream s;
s<<temp2Str;s>>temp2;
s.clear();
s<<temp1Str;s>>temp1;
s.clear(); stringstream s2;
int res=Evaluate(temp1,temp2,tokens[i][]);
s2<<res;
string resStr=s2.str();
container.push(resStr);
}else{
container.push(tokens[i]);
}
}
stringstream s;
int result=;
string reultStr=container.top();
s<<reultStr;
s>>result;
return result;
}
};

Evaluate Reverse Polish Notation(逆波兰式)的更多相关文章

  1. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  2. Java Evaluate Reverse Polish Notation(逆波兰式)

    表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...

  3. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  4. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  5. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

  6. 150. Evaluate Reverse Polish Notation(逆波兰表达式)

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

  7. 150 Evaluate Reverse Polish Notation 逆波兰表达式求值

    求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如:  ["2", "1&quo ...

  8. Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...

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

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

随机推荐

  1. 5.1点击4个按钮显示相应的div

    事件:onclick 属性:display,className 用到for语句,index标记,this当前事件 先清空后附加 <!DOCTYPE html><html>< ...

  2. php中读取以及写入文件的方法总结

    ==>读取文件内容(方法一) $fileData = fread($fileStream,filesize($filePath)); 注意: 文本文件读取到网页上显示时,由于换行符不被解释,文本 ...

  3. iOS programming UITabBarController

    iOS programming UITabBarController 1.1 View controllers become more interesting when the user's acti ...

  4. IE主页被恶意修改处理办法

    HKEY_USERS/.DEFAULT/Software/Policies/Microsoft/Internet Explorer/Control Panel 下的DWORD值“homepage”的键 ...

  5. AWT编程时,Button按钮上的中文编程□□□

    今天学到AWT编程时,照着书上的代码打,代码如下: import java.awt.*; public class PanelTest{    public static void main(Stri ...

  6. swift @objc dynamic

    @objc vs @objc dynamic @objc:  Objective-C entry points One can explicitly write @objc on any Swift ...

  7. jQuery 点击查看 收起

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. cpio - 存取归档包中的文件

    总览 (SYNOPSIS) cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] [-O [[user@]host:]a ...

  9. Web应用启动时,后台自动启动一个线程

    (1)前言 前几天,manager问道一个问题:能不能实现类似于cron的后台管理方式.问题解决后,想对这几个问题进行一下简单的总结.以便抛砖引玉!首先简单的提及一下cron. Cron,计划任务,是 ...

  10. 04CSS文本字体及排版

    CSS文本字体 字体——font-family font-family:字体1,字体2,字体3,……:应用font-family属性可以一次定义多个字体,而在浏览器读取字体时, 会按照定义的先后顺序来 ...