leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation
leetcode 逆波兰式求解
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
import java.util.Stack;
public class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<Integer>();
int tmp;
for(int i = 0; i<tokens.length; i++){
if("+".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b+a);
}
else if("-".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b-a);
}
else if("*".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b*a);
}
else if("/".equals(tokens[i])){
int a = stack.pop();
int b = stack.pop();
stack.add(b/a);
}
else{
stack.add(Integer.parseInt(tokens[i]));
} }
return stack.pop();
}
}
根据你波兰式求值。看到逆波兰式可以想到栈,扫描表达式,遇到数字则将数字入栈,遇到运算符,时,则从栈顶弹出两个元素,后弹出的元素在运算符的左边,先弹出的元素在元素符的右边,执行运算,将结果入栈。扫描结束后,栈中的元素只剩下一个,即逆波兰式的值
leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation的更多相关文章
- Convert Expression to Reverse Polish Notation
Given an expression string array, return the Reverse Polish notation of this expression. (remove the ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- 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(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- [leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...
- [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 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
随机推荐
- Eclipse中Project的属性Deployment Assembly(部署程序集)消失了,不存在了,去哪儿了
1. 该项目不是web项目,所以不存在Deployment Assembly 属性.在Eclipse中,怎样将一个非web project变成一个web project? 1)右键项目,选择Proje ...
- Spring Core Programming(Spring核心编程) - AOP Concepts(AOP基本概念)
1. What is aspect-oriented programming?(什么是面向切面编程?) Aspects help to modularize cross-cutting concern ...
- ES6 与 React
Node和NPM/*安装npm*/npm installnpm install <package>npm install -g <package> /*更新安装包*/npm u ...
- python接口自动化测试(八)-unittest-生成测试报告
用例的管理问题解决了后,接下来要考虑的就是报告我问题了,这里生成测试报告主要用到 HTMLTestRunner.py 这个模块,下面简单介绍一下如何使用: 一.下载HTMLTestRunner下载: ...
- java最简单实现Log打印和生成日志文件
导包 1.commons-logging.jar包 下载 2.log4j.jar包 下载 配置log4j 1.在src根目录下创建一个log4j.properties文件. 文件全部内容如下: log ...
- 小程序入门学习Demo
技术:小程序 概述 适合学习小程序的初级开发人员,入门教程 详细 代码下载:http://www.demodashi.com/demo/14956.html 小程序周边美甲美发预约Demo 代码主 ...
- MySQL技术内幕读书笔记(一)——Mysql体系结构和存储引擎
目录 MySQL体系结构和存储引擎 定义数据库和实例 MYSQL体系结构 MYSQL存储引擎 MySQL体系结构和存储引擎 定义数据库和实例 数据库:物理操作系统文件或者其他形式文件类型的结合.在MY ...
- Android开发导出apk报错:Unable to build: the file dx.jar was not loaded from the SDK folder
问题背景 此问题一般出现在,同时使用了Eclipse和Android Studio,eclipse是不会去下载最新的Android的相关tools,但是studio有时候会自动更新最新的build-t ...
- Windows Server 2008 IIS安装FTP及端口配置
添加角色IIS,选择上FTP服务 打开IIS,右击网站,添加FTP站点 允许访问的指定用户,必须是Windows系统真实存在的用户,为了安全起见,此用户只赋予user组即可,不能赋予远程桌面权限 如果 ...
- 关于python协程中aiorwlock 使用问题
最近工作中多个项目都开始用asyncio aiohttp aiomysql aioredis ,其实也是更好的用python的协程,但是使用的过程中也是遇到了很多问题,最近遇到的就是 关于aiorwl ...