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
原创粉丝点击