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. end()

    end() V1.0概述 回到最近的一个"破坏性"操作之前.即,将匹配的元素列表变为前一次的状态. 如果之前没有破坏性操作,则返回一个空集.所谓的"破坏性"就是 ...

  2. [Luogu] 文艺平衡树(Splay)

    题面:https://www.luogu.org/problemnew/show/P3391 题解:https://www.zybuluo.com/wsndy-xx/note/1138892

  3. 第四届西安邮电大学acm-icpc校赛 猜球球

    题目描述 六一到了,为了庆祝这个节日,好多商家都推出了很多好玩的小游戏.Tongtong看到了一个猜球球的游戏,有n种除了颜色之外完全相同的球,商家从中拿出来一个球球放到了箱子里,已知第i种颜色的球出 ...

  4. 如何写出好的PRD(产品需求文档)(转)

    作者:Cherry,2007年进入腾讯公司,一直从事互联网广告产品管理工作,目前在SNG/效果广告平台部从事效果广告的产品运营工作. PRD(Product Requirement Document, ...

  5. Jury Compromise

    K - Jury Compromise 参考:ACM POJ 1015 Jury Compromise(陪审团的人选,动态规划题,难) 说实话真有点难想,用一个DP[i][j]来表示在选取i个人,辩控 ...

  6. Linux环境下Gitblit服务搭建及秘钥配置

    一.安装gitblit服务 1.下载地址 https://pan.baidu.com/s/1wQ3TEE_gw5xZvyFPZB9xFg 2.上传至linux服务器并解压缩 tar xvf gitbl ...

  7. Druid数据源监控配置

    在web.xml中添加如下代码 <!-- druid监控 --> <servlet> <servlet-name>DruidStatView</servlet ...

  8. LeetCode 10. 正则表达式匹配(Regular Expression Matching)

    题目描述 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s ...

  9. fastadmin 全手动添加规则

    全手动的话 你需要在规则管理里面添加规则 然后角色组就可以设置权限了如果是自动的话 应该就都生成了权限设置 1.增删改查的规则也要添加哦 2.角色组勾选相应的规则 https://ask.fastad ...

  10. Mac配置jdk以及maven

    一 Mac配置JDK和Maven 1.安装成功jdk后 2.打开终端后,输入vim ~/.bash_profile 3.输入: export MAVEN_HOME=/usr/local/maven/a ...