Given an expression string array, return the final result of this expression

Have you met this question in a real interview? Yes
Example
For the expression 2*6-(23+7)/(1+2),
input is [
"2", "*", "6", "-", "(",
"23", "+", "7", ")", "/",
(", "1", "+", "2", ")"
],
return 2 Note
The expression contains only integer, +, -, *, /, (, ).

这道题其实应该算Basic Calculator III. 参考了http://blog.csdn.net/nicaishibiantai/article/details/45740649

思路就是两个stack,一个存数字一个存符号。如果遇到数字直接存到数字stack;如果遇到符号,有几种情况:

1.当前符号比上一个符号优先级高,比如* 高于+,那么直接进栈

2.当前符号低于上一个,那么就要把所有已经在stack里面优先于当前符号的全算完,再推进当前符号

3.当前符号是“(”,直接push

4.当前符号是“)”,就要把所有“(”以前的符号全部算完

 public class Solution {
/**
* @param expression: an array of strings;
* @return: an integer
*/
public int evaluateExpression(String[] expression) {
// write your code here
Stack<Integer> integers = new Stack<Integer>();
Stack<String> ops = new Stack<String>();
int i = 0;
while (i < expression.length) {
String cur = expression[i];
if (isOp(cur)) { // current string is an op
if (cur.equals("(")) ops.push(cur);
else if (cur.equals(")")) {
while (ops.size()>0 && !ops.peek().equals("(")) {
integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
}
ops.pop();
}
else { // +,-,*,/
while (ops.size()>0 && precede(cur, ops.peek())) {
integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
}
ops.push(cur);
}
}
else integers.push(Integer.parseInt(cur)); // current String is an integer, push to integer stack
i++;
} while (!ops.isEmpty()) {
integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
}
return integers.isEmpty()? 0 : integers.pop();
} public boolean isOp(String input) {
if (input.equals("+") || input.equals("-") || input.equals("*")
|| input.equals("/") || input.equals("(") || input.equals(")"))
return true;
return false;
} public int calc(int a, int b, String op) {
if (op.equals("+")) return a+b;
else if (op.equals("-")) return b-a;
else if (op.equals("*")) return a*b;
else return b/a;
} public boolean precede(String a, String b) {
if (b.equals("*") || b.equals("/")) return true;
if (b.equals("+") || b.equals("-")) {
if (a.equals("*") || a.equals("/")) return false;
else return true;
}
return false; //case like (a+b) 到第一个+号时,+和(比应该return false
}
};

Lintcode: Expression Evaluation (Basic Calculator III)的更多相关文章

  1. [LeetCode] Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  2. [LeetCode] 772. Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  3. 【leetcode】Basic Calculator III

    题目如下: Implement a basic calculator to evaluate a simple expression string. The expression string may ...

  4. leetcode 772.Basic Calculator III

    这道题就可以结合Basic Calculator中的两种做法了,分别是括号运算和四则运算的,则使用stack作为保持的结果,而使用递归来处理括号内的值的. class Solution { publi ...

  5. LintCode "Expression Evaluation"

    This is sth. for me to learn.. https://github.com/kamyu104/LintCode/blob/master/C++/expression-evalu ...

  6. Basic Calculator I && II && III

    Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...

  7. Basic Calculator - Stack(表达式计算器)

    978. Basic Calculator https://www.lintcode.com/problem/basic-calculator/description public class Sol ...

  8. [LeetCode] Basic Calculator IV 基本计算器之四

    Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...

  9. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

随机推荐

  1. Tomcat安装、启动和配置

    Tomcat服务器介绍 Tomcat是一个免费开发源代码的web应用服务器,具有与IIS.Apache等web服务器一样处理html页面的功能,另外它还是一个Servlet和JSP容器,独立的serv ...

  2. Oracle数据库--SQL

    1.事务(Transaction ) 1)命名事务 set transaction name ‘transaction_name ’; 2)查看事务是否存在 select name from v$tr ...

  3. nginx调优

    Nginx is an open-source Web Server. It is a high-performance HTTP server that uses very low server r ...

  4. 1763 An Essay towards solving a Problem in the Doctrine of Chances

    https://en.wikipedia.org/wiki/An_Essay_towards_solving_a_Problem_in_the_Doctrine_of_Chances

  5. nginx搭建http和rtmp协议的流媒体服务器

    nginx搭建http和rtmp协议的流媒体服务器 时间:2013-09-23 23:52来源:佚名 作者:本站 举报 点击:232次 实验目的:让Nginx支持flv和mp4格式文件,同时支持Rtm ...

  6. ubuntu如何开启root,如何启用Ubuntu中root帐号

    jingyan.baidu.com/article/495ba84116104238b20ede62.html ubuntu如何开启root,如何启用Ubuntu中root帐号 | 浏览:8344 | ...

  7. yii1.1.3主从(多从)、读写分离配置

    从新配置main.php片段 代码如下 ----------------------------------------------------------- 'db'=>array( 'con ...

  8. JS删除数组条目中重复的条目

    [腾讯2015春招web前端开发练习卷] 请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组. Array.prototyp ...

  9. How to Move Magento to a New Server or Domain

    Magento is one of the fastest growing eCommerce platforms on the internet and with its recent acquis ...

  10. Java学习-019-Properties 文件读取实例源代码

    在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可 ...