Lintcode - Convert Expression to Polish Notation
来源:互联网 发布:淘宝好评能修改吗 编辑:程序博客网 时间:2024/06/16 20:56
Given an expression string array, return the Polish notation of this expression. (remove the parentheses)
Example
For the expression [(5 − 6) * 7] (which represented by ["(", "5", "−", "6", ")", "*", "7"]
), the corresponding polish notation is [* - 5 6 7] (which the return value should be ["*", "−", "5", "6", "7"]
).
Clarification
Definition of Polish Notation:
- http://en.wikipedia.org/wiki/Polish_notation
- http://baike.baidu.com/view/7857952.htm
public class Solution { /** * @param expression: A string array * @return: The Polish notation of this expression */ public ArrayList<String> convertToPN(String[] expression) { ArrayList<String> list = new ArrayList<String>(); Stack<String> stack = new Stack<String>(); for (int cur = expression.length - 1; cur >= 0; cur--) { String str = expression[cur]; if (isOp(str)) { if (str.equals(")")) { stack.push(str); } else if (str.equals("(")) { while (!stack.isEmpty()) { String inPar = stack.pop(); if (inPar.equals(")")) { break; } list.add(inPar); } } else { while (!stack.isEmpty() && order(str) < order(stack.peek())) { list.add(stack.pop()); } stack.push(str); } } else { list.add(str); } } while (!stack.isEmpty()) { list.add(stack.pop()); } ArrayList<String> result = new ArrayList<String>(); for (int i = list.size() - 1; i >= 0; i--) { result.add(list.get(i)); } return result; } boolean isOp(String str) { if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("(") || str.equals(")")) { return true; } else { return false; } } int order(String a) { if (a.equals("*") || a.equals("/")) { return 2; } else if (a.equals("+") || a.equals("-")) { return 1; } else { return 0; } }}
0 0
- Lintcode - Convert Expression to Reverse Polish Notation
- Lintcode - Convert Expression to Polish Notation
- [LintCode] Convert Expression to Polish Notation
- [LintCode]Convert Expression to Reverse Polish Notation
- [Lintcode]Evaluate Reverse Polish Notation
- 【LintCode】Evaluate Reverse Polish Notation 逆波兰表达式求值
- Convert infix expression to prefix expression
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Reverse Polish Notation
- calculate reverse polish notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- MyEclipse 2013 字体颜色配置
- Java 8的一些常见用法
- 测试2
- Eclipse中新建Android项目目录详解
- VNCServer 安装与使用 -- 重装服务器系统过程
- Lintcode - Convert Expression to Polish Notation
- 实验报告:运用shingling+minhash+lsh方法对文档相似性进行分析
- Picking Release Parameter
- openGL 学习1
- sgu-243 Broken Chessboard
- 《Linux/UNIX系统编程手册(上、下册)》
- 聘之有道面试读心术之读书笔记
- C++ primer plus 练习7.9
- 黑马程序员-java中堆和栈的区别