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模型配置文 ...
随机推荐
- is(expr|obj|ele|fn)
is(expr|obj|ele|fn) 概述 根据选择器.DOM元素或 jQuery 对象来检测匹配元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true.大理石平台支架 如果没有元素符合 ...
- JVM——内存结构
一.程序计数器/PC寄存器 (Program Counter Registe) 用于保存当前正在执行的程序的内存地址(下一条jvm指令的执行地址),由于Java是支持多线程执行的,所以程序执行的轨迹不 ...
- 什么是nProtect?
nProtect是设计用于保护个人电脑终端不被病毒和黑客程序感染的新概念的基于网络的反黑客和反病毒的工具. 他帮助确保所有输入个人电脑终端的信息在网络上不落入黑客手中. 在最终用户在执行电子贸易时,可 ...
- 百度AI接口调用
创建应用 登录网站 登录www.ai.baidu.com 进入控制台 进入语音技术 创建应用 管理应用 技术文档 SDK开发文档 接口能力 版本更新记录 注意事项 目前本SDK的功能同REST API ...
- SecureCRT的安装与破解,详细过程
使用SecureCRT可以方便用户在windows环境下对linux主机进行管理,这里为大家讲一下SecureCRT的破解方法,仅供大家参考学习: 1.首先在本站下载SecureCRT 7.3.6最新 ...
- 2.1 Go语言基础之运算符
运算符用于在程序运行时执行数学或逻辑运算. 一.运算符 Go 语言内置的运算符有: 1. 算术运算符 2. 关系运算符 3. 逻辑运算符 4. 位运算符 5. 赋值运算符 1.1 算数运算符 运算符 ...
- URL编码和解码
1. 为什么需要编码 当数据不利于处理.存储的时候,就需要对它们进行编码.如对字符进行编码是因为自然语言中的字符不利于计算机处理和存储.对图片信息.视频信息.声音信息进行压缩.优化,将其“格式化”,是 ...
- Linux 的零拷贝技术
目录 文章目录 目录 Linux I/O 缓存背景 零拷贝技术(Zero-Copy) 参考文章 Linux I/O 缓存背景 当请求文件服务器的下载功能时,服务端程序所做的事情是:将服务器磁盘中的文件 ...
- virtualbox迁移虚拟机
我用的Ubuntu16.04,下图为装好virtualbox时安装好系统后默认的存储位置. 促使我想迁移的原因是我的/home下因为虚拟机的存储原因导致/home下还剩1.5M可用空间..... 该目 ...
- generator自动生成数据表
1.先写好自己要创建的字段等: 然后将将上面的在plsql中运行,创建数据表.