Lintcode: Expression Evaluation (Basic Calculator III)

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.当前符号是“)”,就要把所有“(”以前的符号全部算完

 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 };
时间: 2024-10-15 18:34:11

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

Leetcode 772. Basic Calculator III

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . The expression string contains only non-nega

Leetcode solution 772: Basic Calculator III

Problem Statement Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . The expression string cont

LintCode &quot;Expression Evaluation&quot;

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

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

978. Basic Calculator https://www.lintcode.com/problem/basic-calculator/description public class Solution { /** * @param s: the given expression * @return: the result of expression */ public int calculate(String s) { // Write your code here Stack<Int

LeetCode 227. 基本计算器 II(Basic Calculator II)

227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和空格.整数除法仅保留整数部分. LeetCode227. Basic Calculator II中等 示例?1: 输入: "3+2*2" 输出: 7 示例 2: 输入: " 3/2 " 输出: 1 示例 3: 输入: " 3+5 / 2 " 输出:

LeetCode OJ:Basic Calculator(基础计算器)

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given expression is

LeetCode——Basic Calculator II

Description: Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that

[LeetCode][JavaScript]Basic Calculator

Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the giv

Leetcode 227. Basic Calculator II JAVA语言

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex