Prefix to Infix Conversion
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 a Prefix expression, convert it into a Infix 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 between them.
- string = (operand1 + operator + operand2)
- 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 preToInfix(String pre_exp) {
Stack<String> stack = new Stack<>();
int length = pre_exp.length();
for (int i = length - ; i >= ; i--) {
if (isOperator(pre_exp.charAt(i))) {
String op1 = stack.pop();
String op2 = stack.pop();
String temp = "(" + op1 + pre_exp.charAt(i) + op2 + ")";
stack.push(temp);
} else {
stack.push(pre_exp.charAt(i) + "");
}
}
return stack.peek();
}
}
Prefix to Infix Conversion的更多相关文章
- Postfix to Prefix Conversion & Prefix to Postfix Conversion
Postfix to Prefix Conversion Postfix: An expression is called the postfix expression if the operator ...
- Sphinx 2.2.11-release reference manual
1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...
- swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- swift学习笔记之-高级运算符
//高级运算符 import UIKit /*高级运算符(Advanced Operators):位运算符.溢出运算符.优先级和结合性.运算符函数.自定义运算符 位运算符: 1.位运算符可以操作数据结 ...
- Swift2.1 语法指南——高级操作符
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- iOS开发——Swift篇&Swift关键字详细介绍
Swift关键字详细介绍 每一种语言都有相应的关键词,每个关键词都有他独特的作用,来看看swfit中的关键词: 关键词: 用来声明的: “ class, deinit, enum, extension ...
- Swift 学习笔记(五)
126. 协议(Protocols) 协议语法(Protocol Syntax) 属性要求(Property Requirements) 方法要求(Method Requirements) Mutat ...
- 强大的Cmder
why 漂亮,包装并美化了各个shell 带task功能,能记忆,能执行脚本 配合win10的bash,能实现类似xshell的功能 注意点 需要注意的一点,Cmder来源于另外一个项目ConEmu, ...
- py-faster-rcnn 训练参数修改(转)
faster rcnn默认有三种网络模型 ZF(小).VGG_CNN_M_1024(中).VGG16 (大) 训练图片大小为500*500,类别数1. 一. 修改VGG_CNN_M_1024模型配置文 ...
随机推荐
- LinkedBlockingQueue 实现 生产者 消费者
转载:https://blog.csdn.net/sinat_36553913/article/details/79533606 Java中使用LinkedBlockingQueue实现生产者,消费者 ...
- mybatis 多级级联(多级嵌套)
注:笔者这里的嵌套可以用词有点欠缺,忘见谅 需求:用一个查询接口查出其结果集,这里就用伪代码标识要返回前端的类与类之间的关系. class 顶层{ String otherValue; LinkedL ...
- CodeForces 754C Vladik and chat ——(xjbg)
虽然是xjbg的题目,但是并不很好做. 题意不难理解.读入有点麻烦.做法是先正着推每段对话的?可能是谁说的,然后反过来选择即可.正推时,其中vis数组表示谁已经被用过了,cnt表示该组当前可以选择几个 ...
- redis快照关闭了导致不能持久化的问题
在使用redis的时候我们经常会遇到这种bug: Python与Redis交互时,设置数据出现下列报错信息: MISCONF Redis is configured to save RDB s ...
- 我的zshrc文件设置备份
# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin: ...
- Java项目服务器跨域设置
引入jar包 cors-filter-2.6 :http://central.maven.org/maven2/com/thetransactioncompany/cors-filter/2.6/co ...
- Undo Segment/Undo Retention
undo_retention简单定义,就是最多数据的最少保留时间.AUM模式下,undo_retention参数用于事务commit后undo数据保留的时间.单位为秒.这是个no guarantee的 ...
- Laravel中Session的使用
以file为示例 1.Http request session方法$request->session()->put('key1','value1');echo $request->s ...
- flutter 安卓再次点击返回退出应用
安卓手机点击实体或者虚拟返回键,会返回上一级,当到达最上层是,点击返回退出应用,为了防止用户连续点击返回,导致应用退出,在用户点击返回到最上层时,如果再次点击返回,第一次不退出,并提升用户再次点击退出 ...
- [spring]@Resource和@Autowired区别对比
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...