Infix : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2).
Example : (A+B) * (C-D)

Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )

Given an Infix expression, convert it into a Prefix expression using two stacks.

Examples:

Input : A * B + C / D
Output : + * A B/ C D Input : (A - B/C) * (A/K-L)
Output : *-A/BC-/AKL
分析:
  1. Traverse the infix expression and check if given character is an operator or an operand.
  2. If it is an operand, then push it into operand stack.
  3. If it is an operator, then check if priority of current operator is greater than or less than or equal to the operator at top of the stack. If priority is greater, then push operator into operator stack. Otherwise pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until priority of current operator is less than or equal to operator at top of the operator stack.
  4. If current character is ‘(‘, then push it into operator stack.
  5. If current character is ‘)’, then check if top of operator stack is opening bracket or not. If not pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until top of operator stack is an opening bracket.
  6. The final prefix expression is present at top of operand stack.
 class Solution {
boolean isOperator(char C) {
return C == '-' || C == '+' || C == '*' || C == '/' || C == '^';
} int getPriority(char C) {
if (C == '-' || C == '+') {
return ;
} else if (C == '*' || C == '/') {
return ;
} else if (C == '^') {
return ;
} else {
return ;
}
}
String infixToPrefix(String infix) {
Stack<Character> operators = new Stack<>();
Stack<String> operands = new Stack<String>(); for (int i = ; i < infix.length(); i++) {
if (infix.charAt(i) == '(') {
operators.push(infix.charAt(i));
}
else if (infix.charAt(i) == ')') {
while (!operators.empty() && operators.peek() != '(') {
String op1 = operands.pop();
String op2 = operands.pop(); char op = operators.pop();
String tmp = op + op2 + op1;
operands.push(tmp);
}
} else if (!isOperator(infix.charAt(i))) {
operands.push(infix.charAt(i) + "");
}
else {
while (!operators.empty() && getPriority(infix.charAt(i)) <= getPriority(operators.peek())) {
String op1 = operands.pop();
String op2 = operands.pop(); char op = operators.pop();
operands.push(op + op2 + op1);
}
operators.push(infix.charAt(i));
}
} while (!operators.empty()) {
String op1 = operands.pop();
String op2 = operands.pop(); char op = operators.pop();
operands.push(op + op2 + op1);
}
return operands.peek();
}
}

最新文章

  1. input标签内容改变的触发事件
  2. js获取样式的兼容写法
  3. Mac搭建本地svn服务器,并用Cornerstone连接服务器
  4. js原型解析
  5. WCF - 序列化
  6. 解密:LL与LR解析 2(译,完结)
  7. 利用d3.js绘制中国地图
  8. 定制jackson的自定义序列化(null值的处理)
  9. (转)java匿名内部类详解
  10. Spring Cloud Eureka 自我保护机制
  11. Vue2.0搭建脚手架流程
  12. [idea] SpringBoot整合swagger2实现CRUD
  13. MySQL数据库开发的三十六条军规
  14. scatter参数
  15. centos安装php7.2环境
  16. normalized
  17. WebDriver高级应用实例(10)
  18. Java IO详解(七)------随机访问文件流
  19. 迁移TFS,批量将文档导入SharePoint 2013 文档库
  20. ostream_iterator的可能实现

热门文章

  1. trigger(type,[data]) 在每一个匹配的元素上触发某类事件。
  2. BZOJ 1097: [POI2007]旅游景点atr 状态压缩+Dijkstra
  3. codeforces364D
  4. 安装conda后取消命令行前出现的base,取消每次启动自动激活conda的基础环境, 使用ubuntu 自带的Python环境
  5. Centos7 yum安装mysql(完整版)
  6. mysql zip方式安装
  7. ThinkPHP数据查询与添加语句
  8. 慎用String.intern()作为synchronized的对象锁
  9. SAN LAN MAN WAN的区别
  10. RabbitMQ学习之:(六)Direct Exchange (转贴+我的评论)