1. Given an expression string array, return the final result of this expression
  2.  
  3. Have you met this question in a real interview? Yes
  4. Example
  5. For the expression 2*6-(23+7)/(1+2),
  6. input is
  7.  
  8. [
  9. "2", "*", "6", "-", "(",
  10. "23", "+", "7", ")", "/",
  11. (", "1", "+", "2", ")"
  12. ],
  13. return 2
  14.  
  15. Note
  16. The expression contains only integer, +, -, *, /, (, ).

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

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

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

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

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

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

  1. public class Solution {
  2. /**
  3. * @param expression: an array of strings;
  4. * @return: an integer
  5. */
  6. public int evaluateExpression(String[] expression) {
  7. // write your code here
  8. Stack<Integer> integers = new Stack<Integer>();
  9. Stack<String> ops = new Stack<String>();
  10. int i = 0;
  11. while (i < expression.length) {
  12. String cur = expression[i];
  13. if (isOp(cur)) { // current string is an op
  14. if (cur.equals("(")) ops.push(cur);
  15. else if (cur.equals(")")) {
  16. while (ops.size()>0 && !ops.peek().equals("(")) {
  17. integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
  18. }
  19. ops.pop();
  20. }
  21. else { // +,-,*,/
  22. while (ops.size()>0 && precede(cur, ops.peek())) {
  23. integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
  24. }
  25. ops.push(cur);
  26. }
  27. }
  28. else integers.push(Integer.parseInt(cur)); // current String is an integer, push to integer stack
  29. i++;
  30. }
  31.  
  32. while (!ops.isEmpty()) {
  33. integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
  34. }
  35. return integers.isEmpty()? 0 : integers.pop();
  36. }
  37.  
  38. public boolean isOp(String input) {
  39. if (input.equals("+") || input.equals("-") || input.equals("*")
  40. || input.equals("/") || input.equals("(") || input.equals(")"))
  41. return true;
  42. return false;
  43. }
  44.  
  45. public int calc(int a, int b, String op) {
  46. if (op.equals("+")) return a+b;
  47. else if (op.equals("-")) return b-a;
  48. else if (op.equals("*")) return a*b;
  49. else return b/a;
  50. }
  51.  
  52. public boolean precede(String a, String b) {
  53. if (b.equals("*") || b.equals("/")) return true;
  54. if (b.equals("+") || b.equals("-")) {
  55. if (a.equals("*") || a.equals("/")) return false;
  56. else return true;
  57. }
  58. return false; //case like (a+b) 到第一个+号时,+和(比应该return false
  59. }
  60. };

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. javaWeb中struts开发——Bean标签

    1.struts标签库中常用标签 使用myeclise标签可以自动注入,其中,前三个是经常使用的,主要的是logic标签 2.Bean标签 Bean标签主要用来定义和访问JavaBean,在Strut ...

  2. Java IO包装流如何关闭?

      问题: (1)JAVA的IO流使用了装饰模式,关闭最外面的流的时候会自动调用被包装的流的close()方吗? (2)如果按顺序关闭流,是从内层流到外层流关闭还是从外层到内存关闭? 问题(1)解释: ...

  3. ServletDemo

    1. Servlet 接口 继承 Servlet 接口,实现Servlet 接口的 所有抽象方法! 实现类代码 package xw.servlet; import java.io.IOExcepti ...

  4. PIC12F629帮我用C语言写个程序,控制三个LED亮灭

    http://power.baidu.com/question/240873584599025684.html?entry=browse_difficult PIC12F629帮我用C语言写个程序,控 ...

  5. transform animation transition css3动画

    transform 定义   transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 值 应用  如果transform与transition联合起 ...

  6. Debug BLE application with nRF Sniffer+wireshark

    1. Introduction The nRF Bluetooth® Smart Sniffer is a tool for debugging Bluetooth low energy (BLE) ...

  7. Div自适应高度的方法

    http://www.yutheme.cn/website/index.php/content/view/39/63.html div高度自适应是个比较麻烦的问题,在朋友artery那里看到这个文章, ...

  8. php--字符串函数分类总结

    PHP语言中的字符串函数也是一个比较易懂的知识.今天我们就为大家总结了将近12种PHP字符串函数,希望对又需要的朋友有所帮助,增加读者朋友的PHP知识库.   1.查找字符位置函数 strpos($s ...

  9. readonly=“readonly”与readonly=“true”

    <input id="u" readonly /> <input id="u" readonly="readonly" / ...

  10. 设计模式:享元模式(Flyweight)

    定   义:运用共享技术有效地支持大量细粒度的对象. 结构图: 内部状态:在享元对象内部并且不会随环境而改变的共享部分. 外部状态:随环境改变而改变的.不可共享的状态. Flyweight类,具体享元 ...