Convert Expression to Reverse Polish Notation
Given an expression string array, return the Reverse Polish notation of this expression. (remove the parentheses)
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的更多相关文章
- 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 ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- leetcode150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 11. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- Spark 实践——音乐推荐和 Audioscrobbler 数据集
本文基于<Spark 高级数据分析>第3章 用音乐推荐和Audioscrobbler数据 完整代码见 https://github.com/libaoquan95/aasPractice/ ...
- JS基础(一)异常错误
EvalError(运算错误): raised when an error occurs executing code in eval() RangeError(范围错误): raised when ...
- ESXi主机性能问题
服务器遇到一个问题 百度了下 基本发现是 四路的 windows 服务器的问题. 造成一些 性能降低. 然后查看了下几个虚拟机 的确是设置的4个虚拟插槽 根据百度的结果 要么改配置文件 要么改 这个四 ...
- 3分钟带你搞懂ES6 import 和 export
如下语句是 default import: // B.js import A from './A' 且只在A存在 default export 时生效: // A.js export default ...
- EntityFramework中Json序列化的循环引用问题解决--Newtonsoft.Json
1.在使用EF时,由于数据库主外键关联,将对象进行Json序列化时会遇到循环引用的问题 //EF 中由于数据库主外键关联,对象的序列化经常出现循环引用问题 //使用.Net 自带的序列化工具,序列化出 ...
- stacking算法原理及代码
stacking算法原理 1:对于Model1,将训练集D分为k份,对于每一份,用剩余数据集训练模型,然后预测出这一份的结果 2:重复上面步骤,直到每一份都预测出来.得到次级模型的训练集 3:得到k份 ...
- Django时间时区问题(received a naive datetime while time zone support is active)
在django1.4以后,存在两个概念 naive time 与 active time. 简单点讲,naive time就是不带时区的时间,Active time就是带时区的时间. 举例来说,使用d ...
- idea log4j 用法
1.导入jar包 这里用的maven导入 <!-- LOGGING begin --> <dependency> <groupId>org.slf4j</gr ...
- MT【197】存在$a,b$对于任意的$x$
已知$f(x)=ax^2+bx-\dfrac{1}{4}$,若存在$a,b\in R$,使得对于任意的$x\in[0,7],|f(x)|\le2$恒成立,求$|a|$的最大值____ 提示:$|ax^ ...
- root和alias的区别
先来看看官方说明: root 的用法: location /request_path/image/ { root /local_path/; } 当客户端请求 /request_path/image/ ...