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 ...
随机推荐
- Django根据现有数据库建立/更新model
Django引入外部数据库还是比较方便的,步骤如下: 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自动 ...
- VMware中虚拟机与主机不能ping通解决办法
先去看看服务全部启动了没? VMware相关服务启动关闭脚本 启动了还报错,接着往下看...... 一.如果是桥接模式,那么 可能性1:虚拟机防火墙禁ping,请关闭虚拟机防火墙重试: ...
- Java驱动远程连接mongoDB(简明易懂版)
mongodb默认是不能远程连接的,而且在linux安装完你会发现,它的目录极其简单,连个配置文件都没有. 我的mongodb的版本是3.6,目前最新的.https://www.mongodb.com ...
- The module is an Android project without build variants, and cannot be built
导入 安卓项目报错 Error:The module 'app' is an Android project without build variants, and cannot be built. ...
- linux内核剖析(十一)进程间通信之-共享内存Shared Memory
共享内存 共享内存是进程间通信中最简单的方式之一. 共享内存是系统出于多个进程之间通讯的考虑,而预留的的一块内存区. 共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程 ...
- 设置GRUB密码以防止单用户模式下root密码被恶意更改
在使用LInux系统的时候可能会发生忘记root密码的情况,通常管理员会进入单用户模式下进行重置root密码.那么问题来了,既然管理员可以进入单用户模式,如果恶意用户可以接触的到计算机的话毫无疑问也是 ...
- 用VSCode写Vue要用到的配置
[本文出自天外归云的博客园] 文件-首选项-设置-打开settings.json-用户设置区域填写: { "workbench.colorTheme": "Monokai ...
- MVC教程四:Controller向View传值的几种方式
一.通过ViewData传值 MVC从开始版本就一直支持使用ViewData将Controller里面的数据传递到View.ViewData定义如下: 从上面的截图中可以看出,ViewData里面存的 ...
- websocket Tomcat JSP Demo
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- Unable to install SQL Server (setup.exe), VS Shell installation has failed with exit code 1638.
The problem is likely that there's a newer version of the Visual C++ Redistributable than SQL Server ...