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 +, -, ...
随机推荐
- A brief introduction of myself
来到博客园的第二天.晚上天色墨蓝,余热未退,北京这里的秋风干松爽利,和小组的伙伴们吃了一顿饱饱的香锅,按照咱们国人的传统,所有的事情在饭桌上都解决了,包括队员之间相互的认识和短期任务的分配以及后期的团 ...
- 从零开始学Kotlin-扩展函数(10)
从零开始学Kotlin基础篇系列文章 什么是扩展函数 扩展函数数是指在一个类上增加一种新的行为,我们甚至没有这个类代码的访问权限: Kotlin 可以对一个类的属性和方法进行扩展,且不需要继承或使用 ...
- T检验在项目上的具体实施
我觉得 T 检验,应该用在 判断某种仿真条件因素 对碳纳米管的随机性 是否有显著影响 上.所以不是针对<相同仿真条件对不同源的影响>这个表中的数据做 T 检验 如:判断 金属/半导体比率 ...
- python文本替换
file_data = '' str1 = ' str2 = ' with open(loginfofile, 'r+') as f: #打开文件,r+模式,读取 for line in f: if ...
- 51nod 1295 XOR key (可持久化Trie树)
1295 XOR key 题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查 ...
- c++11 追踪返回类型
c++11 追踪返回类型 返回类型后置:使用"->"符号,在函数名和参数列表后面指定返回类型. #define _CRT_SECURE_NO_WARNINGS #includ ...
- 常用日期计算SQL语句
-- 本月的第一天 SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) -- 本月的最后一天 SELECT DATEADD(ms,-3,DATEADD(mm ...
- Flask script 内的Shell 类 使用
1.集成Python shell 每次自动shell会话都要导入数据库实例和模型,很烦人.为了避免一直重复导入,我们可以做些配置让Flask-Script的Shell命令自动导入特定的对象.若想把对象 ...
- Kubernetes Pod详解
目录 基本概念 pod资源配额 容器的健康检查 静态pod 基本概念 Pod是kubernetes集群中最基本的资源对象.每个pod由一个或多个业务容器和一个根容器(Pause容器)组成.Kubern ...
- webpack进阶--打包
上一片博文主要让大家了解下究竟webpack是干什么的,明显它是专注于打包的. gulp 和 webpack 的区别 gulp,要求我们一步步写task(es6编译.css压缩.图片压缩.打包. ...