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

Valid operators are +-*/. Each operand may be an integer or another expression.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

思路

I would use stack to help solving this problem

Traverse the whole given string array

1. if operand is integer,  push into stack

2. if operand is operation, pop two items from stack, do caculation and push result back to stack

code

 class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String s : tokens) {
if(s.equals("+")) {
stack.push(stack.pop()+stack.pop());
}else if(s.equals("/")) {
int latter = stack.pop();
int former = stack.pop();
stack.push( former / latter);
}else if(s.equals("*")) {
stack.push(stack.pop() * stack.pop());
}else if(s.equals("-")) {
int latter = stack.pop();
int former = stack.pop();
stack.push(former - latter);
}else {
stack.push(Integer.parseInt(s));
}
}
return stack.pop();
}
}

[leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法的更多相关文章

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

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

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

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

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

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

  4. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

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

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

  6. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  7. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  8. Evaluate Reverse Polish Notation(逆波兰式)

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

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

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

随机推荐

  1. string函数的一些实现

    /************************************************************************* > File Name: test.cpp ...

  2. oracle命令2

    使用DDL创建和管理表 DBA角色:拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构,并且系统权限也需要DBA授出,且DBA用户可以操作全体用户的任意基表,包括删除 grant dba t ...

  3. Docker之 默认桥接网络与自定义桥接网卡

    docker引擎会默认创建一个docker0网桥,它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和宿主机都放到同一个二层网络. 1. docker如何使用网桥 1.1 Linux虚拟网桥的特点 ...

  4. docker 进程监控 Dumb-Init进程信号处理 --转自https://blog.csdn.net/tiger435/article/details/54971929

    随着docker及Kubernetes技术发展的越来越成熟稳定,越来越多的公司开始将docker用于生产环境的部署,相比起物理机上直接部署,多了一层docker容器的环境,这就带来一个问题:进程信号接 ...

  5. HBuilder后台保活开发(后台自动运行,定期记录定位数据)

    http://ask.dcloud.net.cn/question/28090 后台自动运行,定期记录定位数据 分类:HTML5+   各位新年好 小弟以前用hbuilder开发过几个项目,现在有一新 ...

  6. 12月中旬项目中出现的几个bug解决方法的思考

    这周做的项目遇到2个费了很多时间才解决的bug,解决之后,发现根本问题并不是什么很难的技术难点,都是因为自己在写代码的过程中,思维不够清晰.还有一个需要再提高的地方就是解决问题的思维,如何快速定位到问 ...

  7. 学习笔记之Model selection and evaluation

    学习笔记之scikit-learn - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/9997485.html 3. Model selection ...

  8. IDEA忽略某些文件

    Settings→Editor→File Types 在下方的忽略文件和目录(Ignore files and folders)中添加自己需要过滤的内容   下图为我自己添加过滤的内容,例如:*.im ...

  9. vmware中的linux虚拟机配置以nat模式上网,并用xshell连接该虚拟机

    1.  首先确保宿主机上的vmnet8处于启用状态 2.  以管理员身份运行vmware >> 编辑 >> 虚拟机网络编辑器 >> 选中Vmnet8 >> ...

  10. win10安装git

    Git官网下载:https://git-scm.com/download 选择win系统,下载.然后选择安装路径,一路next.安装成功后 打开cmd 输入 git --version 配置信息 设置 ...