Given an expression string array, return the Reverse Polish notation of this expression. (remove the parentheses)

Example

For the expression [3 - 4 + 5] (which denote by ["3", "-", "4", "+", "5"]), return [3 4 - 5 +] (which denote by ["3", "4", "-", "5", "+"])

 public class Solution {
public List<String> convertToRPN(String[] expression) {
List<String> list = new ArrayList<>();
Stack<String> stack = new Stack<>(); for (String str : expression) {
if (isOperator(str)) {
if (str.equals("(")) {
stack.push(str);
} else if (str.equals(")")) {
while (!stack.isEmpty() && !stack.peek().equals("(")) {
list.add(stack.pop());
}
stack.pop();
} else {
while (!stack.isEmpty() && order(str) <= order(stack.peek())) {
list.add(stack.pop());
}
stack.push(str);
}
} else {
list.add(str);
}
}
while (!stack.isEmpty()) {
list.add(stack.pop());
}
return list;
} private boolean isOperator(String str) {
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("(")
|| str.equals(")")) {
return true;
}
return false;
} private int order(String a) {
if (a.equals("*") || a.equals("/")) {
return ;
} else if (a.equals("+") || a.equals("-")) {
return ;
} else {
return ;
}
}
}

相关问题:Evaluate Reverse Polish Notation ( https://www.cnblogs.com/beiyeqingteng/p/5679265.html )

Convert Expression to Reverse Polish Notation的更多相关文章

  1. 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 ope ...

  2. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  4. leetcode150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. Leetcode Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. 11. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. LeetCode OJ 150. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. 求二维数组联通子数组和的最大值 (联通涂色) beta!

    算法十分臃肿,效率捉鸡,不知用了多少循环,还有bug...任重道远,编程之美. 思想:按行遍历,找出每行的最大子数组.若行间都联通,行最大子数组相加后,再加上独立的正数.若行间不连通,找出较大子路径, ...

  2. 毕业设计---json,Struts,ajax以及JQuery简单案例

    <!-- Struts2的xml文件配置 --><struts> <package name="default" namespace="/& ...

  3. python自定义mininet拓扑

    python自定义mininet拓扑 前言 闲来无聊,想到很早之前都是用GUI来自定义拓扑,这次用python来自定义一下(以前留下的苦果) 转自Mininet 自定义网络拓扑 过程相对简单 实现过程 ...

  4. [From WIKI] IBM Z

    IBM zEnterprise System From Wikipedia, the free encyclopedia     Jump to navigationJump to search Hi ...

  5. OneZero第四周第四次站立会议(2016.4.14)

    1. 时间: 15:00--15:10  共计10分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  6. Chrome Ajax 跨域设置

    一.前言 web 开发中 Ajax 是十分常见的技术,但是在前后端使用接口对接的调试过程中不可避免会碰到跨域问题.今天我给大家介绍一个十分简单有效的方法. 跨域经典错误 二.Chrome 跨域设置 首 ...

  7. FMDB基本操作

    1.以前使用数据库,因为一般就建立一张表,所以都是自己写代码创建,没用过fmdb,这次因为项目中涉及聊天模块,需要多张表格和数据库保存聊天记录 按照以前方法不好操作,就研究了下fmdb,发现确实挺方便 ...

  8. 背包问题的优化(洛谷1776 宝物筛选_NOI导刊)

    背包型dp,但是没有看清数据范围差点认为是水题了,(然后诡异的拿了20分)标解是:2进制优化,比较简单把每一类物品看做若干个相互独立的物品,放在一个另外的数组里,然后全局跑一边01就可以.主要思想是: ...

  9. LOJ#2983. 「WC2019」数树

    传送门 抄题解 \(Task0\),随便做一下,设 \(cnt\) 为相同的边的个数,输出 \(y^{n-cnt}\) \(Task1\),给定其中一棵树 设初始答案为 \(y^n\),首先可以发现, ...

  10. Hadoop上传文件时报错: could only be replicated to 0 nodes instead of minReplication (=1)....

    问题 上传文件到Hadoop异常,报错信息如下: org.apache.hadoop.ipc.RemoteException(java.io.IOException): File /home/inpu ...