博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode] Evaluate Reverse Polish Notation
阅读量:4698 次
发布时间:2019-06-09

本文共 4759 字,大约阅读时间需要 15 分钟。

Evaluate the value of an arithmetic expression in .

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

 

思路:stack的经典题目,遇到数字压栈,遇到符号出栈两个计算再入栈。

 

import java.util.Stack;public class Solution {    public int evalRPN(String[] tokens) {        Stack
stack = new Stack
(); int a, b; for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("+")) { a = stack.pop(); b = stack.pop(); stack.push(b + a); } else if (tokens[i].equals("-")) { a = stack.pop(); b = stack.pop(); stack.push(b - a); } else if (tokens[i].equals("*")) { a = stack.pop(); b = stack.pop(); stack.push(b * a); } else if (tokens[i].equals("/")) { a = stack.pop(); b = stack.pop(); stack.push(b / a); } else { stack.push(Integer.parseInt(tokens[i])); } } return stack.pop(); } public static void main(String[] args) { System.out.println(new Solution().evalRPN(new String[] { "2", "1", "+", "3", "*" })); System.out.println(new Solution().evalRPN(new String[] { "4", "13", "5", "/", "+" })); }}

 

第二遍记录:

  注意对于减法和除法,先pop出来的被减数和被除数,后pop出来的是减数和除数。

 

扩展:

  根据中序表达式生成逆波兰表示法。

 

第三遍记录: 实现了扩展, 中序表达式 =》逆波兰表达式,基本可以实现一个计算器的功能,支持(),+,-,*,/,并且可以扩展。

 

转换思路:遍历中序表达式,对于当前字符

  如果是‘(’,直接压栈;

  如果是')',pop 栈内元素到输出中,直到遇到‘(’。

  如果是0-9,直接输出。

  如果是操作符(+-*/等),压栈之前把栈顶优先级大于等于自己的全部pop到输出 然后压栈自己

最后将栈内剩余元素pop到输出中。

 

 

import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Stack;public class Solution {    public int calcu(String exp) {        return evalRPN(generateRPN(exp));    }    public int evalRPN(String[] tokens) {        Stack
stack = new Stack
(); int a, b; for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("+")) { a = stack.pop(); b = stack.pop(); stack.push(b + a); } else if (tokens[i].equals("-")) { a = stack.pop(); b = stack.pop(); stack.push(b - a); } else if (tokens[i].equals("*")) { a = stack.pop(); b = stack.pop(); stack.push(b * a); } else if (tokens[i].equals("/")) { a = stack.pop(); b = stack.pop(); stack.push(b / a); } else { stack.push(Integer.parseInt(tokens[i])); } } return stack.pop(); } /** * convert inorder expression to rerverse polish notation * * @param inorder * @return */ public String[] generateRPN(String inorder) { System.out.println("inorder:" + inorder); int n = inorder.length(); Stack
stack = new Stack
(); HashMap
pri = new HashMap
(); config(pri); List
res = new ArrayList
(); for (int i = 0; i < n; i++) { char ch = inorder.charAt(i); if (Character.isSpaceChar(ch)) continue; if (ch == '(') { // for '(' stack.push('('); } else if (ch == ')') { // for ')' char out = 0; while ((out = stack.pop()) != '(') { res.add(out + ""); } } else if (ch >= '0' && ch < '9') { // for operand res.add(ch + ""); } else { // for operator if (stack.isEmpty() || pri.get(ch) > pri.get(stack.peek())) { stack.push(ch); } else { while (!stack.isEmpty() && pri.get(ch) <= pri.get(stack.peek())) { res.add(stack.pop() + ""); } // don't forget this stack.push(ch); } } } // don't forget this while (!stack.isEmpty()) { res.add(stack.pop() + ""); } String[] ret = new String[res.size()]; res.toArray(ret); System.out.println("RPN:" + Arrays.toString(ret)); return ret; } /** * config the priority * * @param map */ private void config(HashMap
map) { map.put('+', 1); map.put('-', 1); map.put('*', 2); map.put('/', 2); map.put('(', Integer.MIN_VALUE); } public static void main(String[] args) { System.out.println(new Solution().calcu("((2 + 1) * 3)")); System.out.println(new Solution().calcu("(4 + (3 / 5))")); System.out.println(new Solution().calcu("(1+2+3)")); System.out.println(new Solution().calcu("1+2*3+(4*5+6)*7")); System.out.println(new Solution().calcu("1+2*3")); System.out.println(new Solution().calcu("1*2+3")); System.out.println(new Solution().calcu("(1+2)*3")); }}

 

 

 

转载于:https://www.cnblogs.com/jdflyfly/p/3830534.html

你可能感兴趣的文章
原生ajax jq跨域
查看>>
在MySQL中操作日期和时间
查看>>
WebService&CXF
查看>>
hibernate框架环境搭建与使用
查看>>
不同大小的字体底部对齐
查看>>
@ControllerAdvice注解的使用
查看>>
pfx2pvk
查看>>
python3 生成随即激活码
查看>>
Codeforces 260 A - A. Laptops
查看>>
Java中System类的相关应用
查看>>
spring mvc(注解)上传文件的简单例子
查看>>
【转】Zend_Json学习
查看>>
洛谷 P3698 [CQOI2017]小Q的棋盘 解题报告
查看>>
洛谷 P1407 [国家集训队]稳定婚姻 解题报告
查看>>
Delphi10.2 Tokyo试用(1)
查看>>
基本数据类型的使用
查看>>
让元素水平和垂直居中的方法总结
查看>>
linux定时执行任务crontab命令用法
查看>>
条件判断_python
查看>>
第二十七天-nfs网络文件系统企业级深度讲解
查看>>