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

  1. Postfix to Prefix Conversion & Prefix to Postfix Conversion

    Postfix to Prefix Conversion Postfix: An expression is called the postfix expression if the operator ...

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

  3. swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  4. swift学习笔记之-高级运算符

    //高级运算符 import UIKit /*高级运算符(Advanced Operators):位运算符.溢出运算符.优先级和结合性.运算符函数.自定义运算符 位运算符: 1.位运算符可以操作数据结 ...

  5. Swift2.1 语法指南——高级操作符

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  6. iOS开发——Swift篇&Swift关键字详细介绍

    Swift关键字详细介绍 每一种语言都有相应的关键词,每个关键词都有他独特的作用,来看看swfit中的关键词: 关键词: 用来声明的: “ class, deinit, enum, extension ...

  7. Swift 学习笔记(五)

    126. 协议(Protocols) 协议语法(Protocol Syntax) 属性要求(Property Requirements) 方法要求(Method Requirements) Mutat ...

  8. 强大的Cmder

    why 漂亮,包装并美化了各个shell 带task功能,能记忆,能执行脚本 配合win10的bash,能实现类似xshell的功能 注意点 需要注意的一点,Cmder来源于另外一个项目ConEmu, ...

  9. py-faster-rcnn 训练参数修改(转)

    faster rcnn默认有三种网络模型 ZF(小).VGG_CNN_M_1024(中).VGG16 (大) 训练图片大小为500*500,类别数1. 一. 修改VGG_CNN_M_1024模型配置文 ...

随机推荐

  1. WebUI自动化之Java语言讲解

    Java学习网站:  default是兜底逻辑,以上条件都不符合时,如何处理.  break是终止循环,continue是终止本次循环:

  2. 学习Javascript的编程风格

    Javascript编程风格   Douglas Crockford是Javascript权威,Json格式就是他的发明. 去年11月他有一个演讲(Youtube),谈到了好的Javascript编程 ...

  3. IN和EXISTS、not in 和not exists的效率详解

    从效率来看: 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1数据量小而T2数据量非常大时,T1<& ...

  4. 【零基础】简单说说一键果体APP的AI

    参考: https://www.jianshu.com/p/8c7a7cb7198c https://blog.csdn.net/gdymind/article/details/82696481 零. ...

  5. LeetCode 25. k个一组翻转链表(Reverse Nodes in k-Group)

    题目描述 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定 ...

  6. article收藏

    sca https://github.com/spring-cloud-incubator/spring-cloud-alibaba spring-cloud-document https://git ...

  7. leetcode16 最接近的三数之和

    做了几周的hard之后,这道题居然轻易就解出来了,稍微debug了一下就ac了,算是有了一丢丢提高把: 思路 这道题因为和三数之和很像,所以充分利用双指针的思想:先排序,然后再固定一个数i,i取值从[ ...

  8. js中bind解析

    一.arguments的含义 // arguments 是一个对应于传递给函数的参数的类数组对象 function a(){ console.log(arguments); } a(); // Arg ...

  9. LeetCode 1154. 一年中的第几天

    给你一个按 YYYY-MM-DD 格式表示日期的字符串 date,请你计算并返回该日期是当年的第几天. 通常情况下,我们认为 1 月 1 日是每年的第 1 天,1 月 2 日是每年的第 2 天,依此类 ...

  10. python之scrapy的FormRequest模拟POST表单自动登陆

    1.FormRequest表单实现自动登陆 # -*- coding: utf-8 -*- import scrapy import re class GithubSpider(scrapy.Spid ...