Total Accepted: 55722 Total Submissions: 249668 Difficulty: Medium

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

class Solution {
public:
int evalRPN(vector<string>& tokens) {
int tokens_size = tokens.size();
int i=,k=;
long long int num1 =,num2=;
while(i<tokens_size){
if(tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" ||tokens[i]=="/"){
num2 = stoi(tokens[--k]);
num1 = stoi(tokens[--k]);
switch(tokens[i][]){
case '+': num1+=num2;break;
case '-': num1-=num2;break;
case '*': num1*=num2;break;
case '/': num1/=num2;break;
}
tokens[k++] = to_string(num1);
i++;
}else{
tokens[k++]=tokens[i++];
}
}
return stoi(tokens[]);
}
};

[stack]Evaluate Reverse Polish Notation的更多相关文章

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

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

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

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

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

  4. leetcode - [2]Evaluate Reverse Polish Notation

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

  5. 【LeetCode】150. Evaluate Reverse Polish Notation

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

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

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

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

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

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

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

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

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

随机推荐

  1. DWZ在APS.NET WebForm中的使用(二)

    任何框架由于个人理解不到位或者框架自身的局限性,在项目实施中,大家或多或少都会遇到一些问题,下面我就讲述下我在使用DWZ开发过程中的几个问题.如有一点能帮助到你,这篇文章也算有存在的意义了. 1.树菜 ...

  2. 查询sybase DB中占用空间最多的前20张表

    按照数据行数查询 name, row_count(db_id(), id) from sysobjects order by row_count(db_id(),id) desc 按照分配的空间查询 ...

  3. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

  4. Azure File SMB3.0文件共享服务(4)

    在Linux上使用Azure文件共享服务 使用SMB 3.0从用户自己的数据连接到Azure,需要加密连接,但目前的Linux SMB客户端都暂时都不支持,Linux的开源社区正在努力将该功能添加到L ...

  5. PHP简单socket编程

    今天再看一点邮件发送的功能,所以了解一下socket变成,看到了一篇不错的文章,转发过来做个笔记吧. 原文链接:http://www.cnblogs.com/thinksasa/archive/201 ...

  6. Myeclipse 2014配置SVN详细图解

    1.什么是SVN? 管理软件开发过程中的版本控制工具. 2.myeclipse安装SVN插件步骤,以myeclipse 2014为例. (1)下载SVN插件 http://subclipse.tigr ...

  7. listvew加载更多

    http://bbs.51cto.com/thread-968277-1.html 又是新的一周的开始,上午自己写了上拉加载更多数据的demo,嘿嘿这里和大家分享.   android开发中,list ...

  8. mobile端

    1.技术解决方案--------->(widget/event/ajax)->function->data------>XMLHttpRequest----->Serve ...

  9. COB Epoxy灌膠時氣泡產生的原因與解決方法

    COB的黑膠 (Epoxy)有氣泡通常是不被允許的,因為外部氣孔不但會影響到外觀,內部氣孔更有可能會破壞 Wire bonding 的鋁線穩定度.既使在COB製程剛完成的時候沒有通過功能測試,也不代表 ...

  10. 续上文----线性表之单链表(C实现)

    本文绪上文线性表之顺序表(C实现) 本文将继续使用单链表实现线性表的另外一种存储结构.这种使用链表实现的存储结构在内存中是不连续的. C实现代码如下: #include<stdio.h> ...