[leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
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逆波兰表示法的更多相关文章
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- 150. Evaluate Reverse Polish Notation(逆波兰表达式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
随机推荐
- 使用VS2015编译grpc_1.3.1
环境: win7_x64,VS2015 开始: 一.安装工具 1. 安装cmake 2. 安装ActivePerl 3. 安装golang 4. 安装nasm 验证安装是否安装成功: cmake -v ...
- spring redis 注解实现缓存机制
1.xml配置 <bean id="poolConfigTax" class="redis.clients.jedis.JedisPoolConfig"& ...
- golang cache--go-cache
go-cache是一款类似于memached 的key/value 缓存软件.它比较适用于单机执行的应用程序. go-cache实质上就是拥有过期时间并且线程安全的map,可以被多个goroutine ...
- PostgreSQL 自定义自动类型转换(CAST)
转载自:https://yq.aliyun.com/articles/228271 背景 PostgreSQL是一个强类型数据库,因此你输入的变量.常量是什么类型,是强绑定的,例如 在调用操作符时,需 ...
- SSB基准测试
SSB(Star Schema Benchmark)是麻省州立大学波士顿校区的研究人员定义的基于现实商业应用的数据模型,业界公认用来模拟决策支持类应用,比较公正和中立.学术界和工业界普遍采用它来评价决 ...
- Qt配置cmake;运行带参数的程序
配置cmake编译器,步骤如下: 步骤1: Qt下新建一个project. 步骤2: 在该project目录下创建一个CMakeLists.txt文件,并按规范编写该文件. Tip: projec ...
- Android入门(一) IDEA上创建Android应用之helloworld
Android入门(一) IDEA上创建Android应用之helloworld 首先看运行结果: 一.准备工作 下载安装IntelliJ IDEA :我这里用的是2018.2.7 下载安装Genym ...
- django 分页和中间件
分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...
- Vue 封装的noData组件
<template> <div :style="{color: fontColor}" :class="['noDataView', iconType] ...
- nodejs模块循环引用讲解
CommonJS 模块的重要特性是加载时执行,即脚本代码在require的时候,就会全部执行.一旦出现某个模块被"循环加载",就只输出已经执行的部分,还未执行的部分不会输出. 让我 ...