Given an expression string array, return the Reverse Polish notation of this expression. (remove the parentheses)

Example

For the expression [3 - 4 + 5] (which denote by ["3", "-", "4", "+", "5"]), return [3 4 - 5 +] (which denote by ["3", "4", "-", "5", "+"])

 public class Solution {
public List<String> convertToRPN(String[] expression) {
List<String> list = new ArrayList<>();
Stack<String> stack = new Stack<>(); for (String str : expression) {
if (isOperator(str)) {
if (str.equals("(")) {
stack.push(str);
} else if (str.equals(")")) {
while (!stack.isEmpty() && !stack.peek().equals("(")) {
list.add(stack.pop());
}
stack.pop();
} 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());
}
return list;
} private boolean isOperator(String str) {
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("(")
|| str.equals(")")) {
return true;
}
return false;
} private int order(String a) {
if (a.equals("*") || a.equals("/")) {
return ;
} else if (a.equals("+") || a.equals("-")) {
return ;
} else {
return ;
}
}
}

相关问题:Evaluate Reverse Polish Notation ( https://www.cnblogs.com/beiyeqingteng/p/5679265.html )

Convert Expression to Reverse Polish Notation的更多相关文章

  1. leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation

    leetcode 逆波兰式求解 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid ope ...

  2. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  4. leetcode150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. Leetcode Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. 11. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. LeetCode OJ 150. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. 《LINUX内核设计与实现》第三周读书笔记——第一二章

    <Linux内核设计与实现>读书笔记--第一二章 20135301张忻 估算学习时间:共2小时 读书:1.5 代码:0 作业:0 博客:0.5 实际学习时间:共2.5小时 读书:2.0 代 ...

  2. linux第一次实验报告

    http://wwwcnblogs.com/20135228guoyao/4964477.html

  3. We're Chronos! ----- Meet the team 团队作业 #1

    Meet Us —————————————————La ligne de séparation————————————————— Kane Lim [林珣玙] < PM, Programmer ...

  4. VS2013 单元测试

    1.打开VS2013 --> 新建一个项目.这里创建一个c#控制台项目.取名为ccj_test1 2.进入控制台项目ccj_test1的Program类,创建一个add静态方法,并将progra ...

  5. AOP 环绕通知 集成了前置 后置 返回通知等功能

    AOP 环绕通知 集成了前置 后置 返回通知等功能

  6. suse11/12关闭防火墙

    suse11关闭操作为:service SuSEfirewall2_setup stopservice SuSEfirewall2_init  stop 取消开机启动防火墙:chkconfig SuS ...

  7. luogu1351 [NOIp2014]联合权值 (dfs)

    有两种情况:一个点到它的父亲的父亲(要算两次).一个点的子节点之间互相到达 #include<bits/stdc++.h> #define pa pair<int,int> # ...

  8. isspace 对含有中文 的字符串进行检查的时候表现不正常!?

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...

  9. ModelMap对象的 addAttribute,put两个方法区别

    这个是 源码中 ModelMap的定义 类 public class ModelMap extends LinkedHashMap<String, Object> 说明 ModelMap是 ...

  10. spring注入时报错::No qualifying bean of type 'xxx.xxMapper'

    做一个小项目,因为有 baseService,所以偷懒就没有写单独的每个xxService接口,直接写的xxServiceImpl,结果在service实现类中注入Mapper的时候,用的 @Auto ...