Postfix to Prefix Conversion & Prefix to Postfix Conversion
Postfix to Prefix Conversion
Postfix: An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator).
Example : AB+CD-* (Infix : (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 a Postfix expression, convert it into a Prefix expression.
分析:
- Read the Postfix expression from left to right
- If the symbol is an operand, then push it onto the Stack
- If the symbol is an operator, then pop two operands from the Stack
- Create a string by concatenating the two operands and the operator before them. Like: string = operator + operand2 + operand1, and push the resultant string back to Stack
- Repeat the above steps until end of Prefix expression.
class Solution {
static boolean isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
default:
return false;
}
} static String postToPre(String exp) {
Stack<String> s = new Stack<>();
int length = exp.length();
for (int i = ; i < length; i++) {
if (isOperator(exp.charAt(i))) {
String op1 = s.pop();
String op2 = s.pop();
String temp = exp.charAt(i) + op2 + op1;
s.push(temp);
}
else {
s.push(exp.charAt(i) + "");
}
}
return s.peek();
}
}
Prefix to Postfix Conversion
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) )
Postfix: An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator).
Example : AB+CD-* (Infix : (A+B * (C-D) )
Given a Prefix expression, convert it into a Postfix expression.
分析:
- Read the Prefix expression in reverse order (from right to left)
- If the symbol is an operand, then push it onto the Stack
- If the symbol is an operator, then pop two operands from the Stack
- Create a string by concatenating the two operands and the operator after them.
- string = operand1 + operand2 + operator
- And push the resultant string back to Stack
- Repeat the above steps until end of Prefix expression.
class Solution {
boolean isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
default:
return false;
}
} String preToPost(String pre_exp) {
Stack<String> s = new Stack<>();
int length = pre_exp.length();
for (int i = length - ; i >= ; i--) {
if (isOperator(pre_exp.charAt(i))) {
String op1 = s.pop();
String op2 = s.pop();
String temp = op1 + op2 + pre_exp.charAt(i);
s.push(temp);
} else {
s.push(pre_exp.charAt(i) + "");
}
}
return s.peek();
}
}
Postfix to Prefix Conversion & Prefix to Postfix Conversion的更多相关文章
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- Linux+postfix+extmail+dovecot打造基于web页面的邮件系统
原文地址:http://blog.csdn.net/deansrk/article/details/6717720 最终效果图: 准备阶段:需要手动下载的软件包: postfix-2.6.5.tar. ...
- Postfix 电子邮件系统精要
来源: http://sery.blog.51cto.com/10037/45500 Postfix 电子邮件系统精要 作者:田逸(sery@163.com) from [url]http://ww ...
- CentOS 系统中安装postfix+dovecot+openwebmail <转>
一.先卸载sendmain[root@ser ~]# yum remove sendmail 二.安装postfix ,dovecot,cyrus-sasl[root@ser ~]# yum -y ...
- CentOS 6.4 x64 postfix + dovecot + 虚拟用户认证
第一, 首先必须安装 apacache mysql php CentOS 直接使用 yum 安装 yum -y install httpd httpd-devel mysql php-mysql ...
- Centos7搭建邮件服务器-Postfix+Cyrus-sasl+Courier-authlib+Dovecot+ExtMail+Centos7
1.环境介绍 MTA: Postfix 3.1.4 SASL: Cyrus-sasl 2.1.26 ; Courier-authlib 0.66.1(Cyrus-sasl使用Courier-authl ...
- Linux下开源邮件系统Postfix+Extmail+Extman环境部署记录
一.基础知识梳理MUA (Mail User Agent) MUA 既是"邮件使用者代理人",因为除非你可以直接利用类似 telnet 之类的软件登入邮件主机来主动发出信件,否则您 ...
- Postfix - Extmail 邮箱系统
Postfix Dovecot Extmail 邮箱系统早前的内部邮箱系统重新整理下:现在Extmail官方有集成镜像的EMOS_1.6_x86_64免费版:可直接下载安装: 系统环境: linux ...
- postfix邮件服务器搭建02-安装篇
本文接着上文的环境,进行postfix邮件发信端和dovecot邮件收信端的部署,之后部署基于浏览器的extmail图形管理端,使管理员可以通过网页对邮件虚拟用户进行管理,对邮件服务器进行管控 1.p ...
随机推荐
- prevAll([expr]) 查找当前元素之前所有的同辈元素
prevAll([expr]) 概述 查找当前元素之前所有的同辈元素 可以用表达式过滤.大理石构件维修 参数 exprStringV1.2 用于过滤的表达式 示例 描述: 给最后一个之前的所有div加 ...
- 「BZOJ 5161」最长上升子序列「状压DP」
题意 求一个\(1\sim n\)的排列LIS的期望长度,\(n\leq 28\) 题解 考虑朴素的LIS:\(f[i] = min(f[j]) + 1\) 记\(mx[i]\)为\(f\)的前缀最大 ...
- JavaWeb_(Hibernate框架)Hibernate中数据查询语句Criteria基本用法
Criteria进行数据查询与HQL和SQL的区别是Criteria完全是面向对象的方式在进行数据查询,将不再看到有sql语句的痕迹,使用Criteria 查询数据包括以下步骤: 1. 通过sessi ...
- NSPredicate谓词的用法
在IOS开发Cocoa框架中提供了一个功能强大的类NSPredicate,下面来讨论一下它的强大之处在哪...NSPredicate继承自NSObject,它有两个派生的子类• NSCompariso ...
- IO之复制文件的四种方式
1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II)
题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 m ...
- R语言:载入rjava(xlsx)包报错
先安装JRE,在电脑中添加环境变量: 电脑-右键-属性-高级系统设置-环境变量-用户变量下新建:变量名:JAVA-HOME,变量值:JRE安装路径(到jre1.8***这个文件夹就行了) 系统变量下找 ...
- Mac系统下,docker安装nextcloud,打造个人本地网盘
1.安装docker 推荐下载地址:http://get.daocloud.io/#install-docker-for-mac-windows 2.拉取镜像 $ docker pull next ...
- centos7 开启80端口
关闭与开启防火墙 systemctl stop firewalld.servicesystemctl start firewalld.service 先查看防火墙是否开启的状态,以及开放端口的情况:s ...
- 省份-城市-区域三级联动【struts2 + ajax +非数据库版】
package loaderman; /** * 实体,封装省份和城市 */ public class Bean { private String province;//省份 private Stri ...