Infix expression: The expression of the form a op b. When an operator is in-between every pair of operands.
Postfix expression: The expression of the form a b op. When an operator is followed for every pair of operands.

Input : abc++
Output : (a + (b + c)) Input : ab*c+
Output : ((a*b)+c)
分析
1. Read the next symbol from the input.
2.If the symbol is an operand, push it onto the stack.
3.Otherwise,
…3.1 the symbol is an operator.
…3.2 Pop the top 2 values from the stack.
…3.3 Put the operator, with the values as arguments and form a string.
…3.4 Push the resulted string back to stack.
 class Solution {
boolean isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
default:
return false;
}
} String postToInfix(String exp) {
Stack<String> s = new Stack<String>(); for (int i = ; i < exp.length(); i++) {
if (!isOperator(exp.charAt(i))) {
s.push(exp.charAt(i) + "");
} else {
String op1 = s.pop();
String op2 = s.pop();
s.push("(" + op2 + exp.charAt(i) + op1 + ")");
}
}
return s.peek();
}
}

Postfix to Infix的更多相关文章

  1. UVa 727 - Equation

    题目大意:给一个中缀表达式,转换成后缀表达式. 这类题一直不太会,让我想就是建一棵表达式树,然后后续遍历算了,可是建树的过程实在太麻烦了.今天才看到有中缀表达式转换成后缀表达式的算法,可以用栈进行实现 ...

  2. c经典算法

    1. 河内之塔 说明 河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时 北越的首都,即现在的胡志明市:1883年法国数学家 Ed ...

  3. C++之字符串表达式求值

    关于字符串表达式求值,应该是程序猿们机试或者面试时候常见问题之一,昨天参加国内某IT的机试,压轴便为此题,今天抽空对其进行了研究. 算术表达式中最常见的表示法形式有 中缀.前缀和 后缀表示法.中缀表示 ...

  4. Java经典算法大全

    1.河内之塔.. 2.Algorithm Gossip: 费式数列. 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官(一) 6. ...

  5. Code Project精彩系列(转)

    Code Project精彩系列(转)   Code Project精彩系列(转)   Applications Crafting a C# forms Editor From scratch htt ...

  6. Infix to postfix conversion 中缀表达式转换为后缀表达式

    Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...

  7. Infix to Postfix Expression

    Example : Infix : (A+B) * (C-D) ) Postfix: AB+CD-* 算法: 1. Scan the infix expression from left to rig ...

  8. infix to postfix 完整版

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  9. Infix to postfix without '(' and ')'

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

随机推荐

  1. 小米 oj 发奖励(思维)

     发奖励 序号:#75难度:有挑战时间限制:1000ms内存限制:10M 描述 小明老师准备给一些得到小红花的小朋友发糖果做为奖励. 假设有n个小朋友,每个小朋友拥有的小红花为m(n)个,他让这n个小 ...

  2. MySQL Innodb引擎和MyIASM引擎的区别

    Innodb引擎 Innodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别.该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它本身其实就是基于MySQL ...

  3. Zookeeper原理 二

    Zookeeper到底是什么!? 学一个东西,不搞明白他是什么东西,哪还有心情学啊!! 首先,Zookeeper是Apache的一个java项目,属于Hadoop系统,扮演管理员的角色. 然后看到官网 ...

  4. Zookeeper原理 一

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...

  5. springMVC课程笔记(二)springMVC组件配置

    1.springMVC的DispatcherServlet前段控制器配置,如下图所示在web.xml中配置如下内容: 2.在spring配置文件中,配置处理器适配器HandlerAdapter和映射器 ...

  6. mysql 从一个表中查数据并插入另一个表实现方法

    类别一. 如果两张张表(导出表和目标表)的字段一致,并且希望插入全部数据,可以用这种方法: INSERT INTO  目标表  SELECT  * FROM  来源表 ; 例如,要将 articles ...

  7. PHP遍历目录下的文件夹和文件 以及遍历文件下内容

    1.遍历目录下的文件夹和文件: public function bianli1($dir) { $files = array(); if($head = opendir($dir)) { while( ...

  8. 关于本电脑qt5.11+vs2017+opencv3.4的配置问题

    本人想用qt5.11+vs2017+opencv3.4开发程序,配置了很久才成功,现在把配置后的环境变量记录一下,以供自己以后参考,同时也供大家参考. qt5.11+vs2017+opencv3.4的 ...

  9. github上打开或下载过慢的问题解决

    1.用站长工具查询github.com http://tool.chinaz.com/dns?type=1&host=github.com&ip= 2.找到“美国[海外]”项查到的ip ...

  10. druid 参数配置详解

    druid 参数配置详解 */--> druid 参数配置详解 Table of Contents 1. 初始化连接 2. 参数配置及说明 3. 注意事项 3.1. 底层连接 3.2. 空闲检查 ...