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的更多相关文章

  1. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  2. Linux+postfix+extmail+dovecot打造基于web页面的邮件系统

    原文地址:http://blog.csdn.net/deansrk/article/details/6717720 最终效果图: 准备阶段:需要手动下载的软件包: postfix-2.6.5.tar. ...

  3. Postfix 电子邮件系统精要

    来源: http://sery.blog.51cto.com/10037/45500 Postfix 电子邮件系统精要 作者:田逸(sery@163.com)  from [url]http://ww ...

  4. CentOS 系统中安装postfix+dovecot+openwebmail <转>

    一.先卸载sendmain[root@ser ~]#  yum remove sendmail 二.安装postfix ,dovecot,cyrus-sasl[root@ser ~]#  yum -y ...

  5. CentOS 6.4 x64 postfix + dovecot + 虚拟用户认证

    第一, 首先必须安装 apacache  mysql  php CentOS 直接使用 yum 安装 yum -y install httpd httpd-devel mysql php-mysql  ...

  6. 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 ...

  7. Linux下开源邮件系统Postfix+Extmail+Extman环境部署记录

    一.基础知识梳理MUA (Mail User Agent) MUA 既是"邮件使用者代理人",因为除非你可以直接利用类似 telnet 之类的软件登入邮件主机来主动发出信件,否则您 ...

  8. Postfix - Extmail 邮箱系统

    Postfix Dovecot Extmail 邮箱系统早前的内部邮箱系统重新整理下:现在Extmail官方有集成镜像的EMOS_1.6_x86_64免费版:可直接下载安装: 系统环境: linux ...

  9. postfix邮件服务器搭建02-安装篇

    本文接着上文的环境,进行postfix邮件发信端和dovecot邮件收信端的部署,之后部署基于浏览器的extmail图形管理端,使管理员可以通过网页对邮件虚拟用户进行管理,对邮件服务器进行管控 1.p ...

随机推荐

  1. Luogu P1110 [ZJOI2007]报表统计 multiset

    沿用了学长的$multiset$ 然后这道题可以看到我的程序中有两行注释,它在我看来和他们下面的代码没区别,但是我们发现,C++会先调用后面的参数,所以$--it$会被先执行 ... ... ... ...

  2. FFT算法理解与c语言的实现

    完整内容迁移至 http://www.face2ai.com/DIP-2-3-FFT算法理解与c语言的实现/ http://www.tony4ai.com/DIP-2-3-FFT算法理解与c语言的实现 ...

  3. Python数据抓取(3) —抓取标题、时间及链接

    本次分享,jacky将跟大家分享如何将第一财经文章中的标题.时间以及链接抓取出来 (一)观察元素抓取位置 网页的原始码很复杂,我们必须找到特殊的元素做抽取,怎么找到特殊的元素呢?使用开发者工具检视每篇 ...

  4. K - Kia's Calculation(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. 7. 使用Hystrix实现微服务的容错处理

                  使用Hystrix实现微服务的容错处理 7.1. 实现容错的手段 7.1.1. 雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整 ...

  6. 【Redis 向Redis中批量导入mysql中的数据(亲自测试)】

    转自:https://blog.csdn.net/kenianni/article/details/84910638 有改动,仅供个人学习 问题提出:缓存的冷启动问题 应用系统新版本上线,这时候 re ...

  7. SpringBoot整合guava缓存

    1.pom文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  8. legend3---8、烦请节约时间

    legend3---8.烦请节约时间 一.总结 一句话总结: 时间不要浪费在垃圾情绪和无效社交上面. 1.商标不能以个人的名义注册? 可以先注册个体工商户,然后再可以注册商标 2.注册一个商标大概花多 ...

  9. python监控wechat

    import osimport reimport shutilimport timeimport itchatfrom itchat.content import * # 说明:可以撤回的有文本文字. ...

  10. P3146 [USACO16OPEN]248

    P3146 [USACO16OPEN]248 题解 第一道自己码出的区间DP快庆祝一哈 2048 每次可以合并任意相邻的两个数字,得到的不是翻倍而是+1 dp[L][R] 区间 L~R 合并结果 然后 ...