波兰式解析Evaluate Reverse Polish Notation
来源:互联网 发布:苹果cms采集 编辑:程序博客网 时间:2024/05/16 15:26
本文介绍波兰式解析算法。
Leetcode 原题如下
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
中文翻译如下,有效的操作符包括: +, -, *,/。 每个操作数是一个整数或者是另个一表达式。
例子如上图。
Java实现如下,包括两个版本。
版本一,包括打印解析树,及"((2+1)*3)->".
import java.util.Stack;public class Solution {public static void main(String[] args){String[] input1 = {"2", "1", "+", "3", "*"};String[] input2 = {"4", "13", "5", "/", "+"};Solution s = new Solution();s.evalRPN(input1);s.evalRPN(input2);} class Node{ int val; String sVal; Node(int val, String sval){ this.val = val; this.sVal = sval; } } private boolean isNumber(String str){ return str.matches("-?\\d+"); } private boolean isOperator(String str){ return str.matches("[/+*-]"); } private int compute(int left, int right, char operator){ switch(operator){ case '/': return left / right; case '*': return left * right; case '-': return left - right; case '+': return left + right; //case default return 0;//need to handle this one }return 0; } public int evalRPN(String[] tokens) { Stack<Node> stack = new Stack(); for(int i = 0; i < tokens.length; i++){ String token = tokens[i]; if(isNumber(token)) { int val = Integer.parseInt(token); stack.push(new Node(val, token)); }else if(isOperator(token)){ Node right = stack.pop(); Node left = stack.pop(); int tmp = compute(left.val, right.val, token.charAt(0)); String stmp = "("+left.sVal + token + right.sVal+")"; Node rst = new Node(tmp, stmp); stack.push(rst); } } int rval = 0; if(stack.size() == 1){ Node node = stack.pop(); System.out.println(node.sVal + "->" + node.val); rval = node.val; } return rval; }}
输出结果如下:
((2+1)*3)->9
(4+(13/5))->6
版本二,简化版本,速度更快
public class Solution { private boolean isNumber(String str){ return str.matches("-?\\d+"); } private boolean isOperator(String str){ return str.matches("[/+*-]"); } private int compute(int left, int right, char operator){ switch(operator){ case '/': return left / right; case '*': return left * right; case '-': return left - right; case '+': return left + right; //case default return 0;//need to handle this one }return 0; } public int evalRPN(String[] tokens) { Stack<Integer> stack = new Stack(); for(int i = 0; i < tokens.length; i++){ String token = tokens[i]; if(isNumber(token)) { int val = Integer.parseInt(token); stack.push(val); }else if(isOperator(token)){ int right = stack.pop(); int left = stack.pop(); int tmp = compute(left,right, token.charAt(0)); stack.push(tmp); } } int rval = 0; if(stack.size() == 1){ rval = stack.pop(); } return rval; }}
0 0
- 波兰式解析Evaluate Reverse Polish Notation
- 计算逆波兰式 Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation(逆波兰式求值)
- 计算逆波兰式 (Evaluate Reverse Polish Notation)
- Evaluate Reverse Polish Notation--逆波兰式求值
- 150. Evaluate Reverse Polish Notation 逆波兰式
- 150. Evaluate Reverse Polish Notation(逆波兰式)
- Evaluate Reverse Polish Notation 逆波兰表达式
- LeetCode—Evaluate Reverse Polish Notation 逆波兰写法的解析
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- 辩证思维10级修炼
- JDom读取xml文件例子
- 【ThinkingInC++】70、防御性编程
- Android中适用于ListView、GridView等组件的通用Adapter
- Android之ContentProvider总结
- 波兰式解析Evaluate Reverse Polish Notation
- Android中为TextView添加多个可点击的文本
- 三大开源版本控制平台
- 黑马程序员--银行业务调度系统
- 对面向对象编程中的继承概念的再理解
- BAT收购历史
- Smart Home DIY 计划
- 操作系统概论
- Phalcon 开发工具(Phalcon Developer Tools)