看Java的栈和队列,然后就……写了个转换的Demo(后来发现leetcode上的题是计算后缀表达式的结果,我写了个转换的func就没啥用。
但是好不容易写出来了,删掉又觉得可惜,遂放在这里做一个记录。
public static void main(String[] args) {
String result = changeMedialExpressions();
System.out.println(result);
}
private static String changeMedialExpressions() {
String medialExpressions = "(5+4)*3-2";
String[] strMedialExpressions = medialExpressions.split("");
//改成后缀表达式(逆波兰表达式)
Stack stringStack = new Stack<>(); //数字
Stack symbolsStack = new Stack<>(); //符号
for (int i = 0; i < strMedialExpressions.length; i++) {
List> list = new ArrayList<>();
switch (strMedialExpressions[i]){
case "+": //1
list = getResult(symbolsStack,stringStack,strMedialExpressions[i],1);
break;
case "-": //2
list = getResult(symbolsStack,stringStack,strMedialExpressions[i],2);
break;
case "*": //3
list = getResult(symbolsStack,stringStack,strMedialExpressions[i],3);
break;
case "/": //4
list = getResult(symbolsStack,stringStack,strMedialExpressions[i],4);
break;
case "(": //5
list = getResult(symbolsStack,stringStack,strMedialExpressions[i],5);
break;
case ")": //6
list = getResult(symbolsStack,stringStack,strMedialExpressions[i],6);
break;
default:
stringStack.push(strMedialExpressions[i]); //如果是数字就直接扔进去
list.add(symbolsStack);
list.add(stringStack);
break;
}
symbolsStack = list.get(0);
stringStack = list.get(1);
}
String result = "";
for (int i = 0; i < stringStack.size(); i++) {
/*System.out.print(stringStack.get(i));*/
result+=stringStack.get(i);
}
for (int i = symbolsStack.size()-1; i >=0; i--) {
//System.out.print(symbolsStack.get(i));
result+=symbolsStack.get(i);
}
return result;
}
/*
抽调方法
*/
private static List> getResult(Stack symbolsStack, Stack stringStack, String strMedialExpression, int num) {
List> list = new ArrayList<>();
if(symbolsStack.isEmpty()){
symbolsStack.push(strMedialExpression);
list.add(symbolsStack);
list.add(stringStack);
}else{
//判断优先级
list = checkPriority(symbolsStack,stringStack,strMedialExpression,num);
}
return list;
}
/*
检查优先级
*/
private static List> checkPriority(Stack symbolsStack, Stack stringStack
,String symbols,int priority) {
List> list = new ArrayList<>();
if(priority == 5 || priority == 4 || priority == 3){ //如果是( * /就直接扔进去
symbolsStack.push(symbols);
}
if(priority == 1 || priority == 2){ //+ 和 - 要进行遍历
if(symbolsStack.peek().equals("+") || symbolsStack.peek().equals("-") || symbolsStack.peek().equals("(")){ //如果栈顶元素是 + - ( 就直接扔进去
symbolsStack.push(symbols);
}else{ //如果是 * 或者 /就要 进行插入
List list1 = getSymbolsStack(symbolsStack,symbols,stringStack);
symbolsStack = list1.get(0);
stringStack = list1.get(1);
}
}
if(priority == 6){ //)要连着d出。直到)为止,放入stringStack
//不能一起return 就写在这里
String a = symbolsStack.peek();
while(!a.equals("(")){
stringStack.push(a);
symbolsStack.pop();
a = symbolsStack.peek();
}
symbolsStack.pop();
}
list.add(symbolsStack);
list.add(stringStack);
return list;
}
/*
插入符号
*/
private static List getSymbolsStack(Stack symbolsStack,String symbols,Stack stringStack) {
int num = 0;
if(symbolsStack.isEmpty()){
symbolsStack.push(symbols);
}else{
for (int i = symbolsStack.size()-1; i >=0 ; i--) {
if(symbolsStack.get(i).equals(symbols) || symbolsStack.get(i).equals("+") || symbolsStack.get(i).equals("-")){ //找到了
num = i;
break;
}else{
continue;
}
}
//记下num,然后push
for (int i = symbolsStack.size()-1;i >= num;i--) {
stringStack.push(symbolsStack.peek());
symbolsStack.pop();
}
symbolsStack.push(symbols);
}
List list = new ArrayList<>();
list.add(symbolsStack);
list.add(stringStack);
return list;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)