> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/algorithm-practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/algorithm-practice/leetcode/stack/772-basic-calculator-iii.md).

# 772    Basic Calculator III

## 772. [Basic Calculator III](https://leetcode.com/problems/basic-calculator-iii/description/)

## 1. Question

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-negative integers,`+`,`-`,`*`,`/`operators , open`(`and closing parentheses`)`and empty spaces. The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of`[-2147483648, 2147483647]`.

Some examples:

```
"1 + 1" = 2
" 6-4 / 2 " = 4
"2*(5+5*2)/3+(6/2+8)" = 21
"(2+6* 3+5- (3*14/7+2)*5)+3"=-12
```

**Note:Do not** use the`eval`built-in library function.

## 2. Implementation

**(1) Stack**

```java
class Solution {
    public int calculate(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }

        Stack<Integer> nums = new Stack();
        Stack<Character> operators = new Stack();

        int i = 0;
        int num = 0;
        int n = s.length();

        while (i < n) {
            char c = s.charAt(i);

            if (Character.isDigit(c)) {
                num = c - '0';

                while ((i + 1) < n && Character.isDigit(s.charAt(i + 1))) {
                    num = 10 * num + s.charAt(i + 1) - '0';
                    ++i;
                }
                nums.push(num);
            }
            else if (isOperator(c)) {
                while (!operators.isEmpty() && hasPrecedence(c, operators.peek())) {
                    nums.push(calculate(operators.pop(), nums.pop(), nums.pop()));
                }
                operators.push(c);
            }
            else if (c == '(') {
                operators.push(c);
            }
            else if (c == ')') {
                while (!operators.isEmpty() && operators.peek() != '(') {
                    nums.push(calculate(operators.pop(), nums.pop(), nums.pop()));
                }
                operators.pop();
            }
            ++i;
        }

        while (!operators.isEmpty()) {
            nums.push(calculate(operators.pop(), nums.pop(), nums.pop()));
        }
        return nums.isEmpty() ? 0 : nums.pop();
    }

    public boolean isOperator(char c) {
        return c == '+' || c == '-' || c == '*' || c == '/';
    }

    public int calculate(char operator, int num1, int num2) {
        int res = 0;

        switch(operator) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
        }
        return res;
    }

    // check if op2 has higher precendence than op1
    public boolean hasPrecedence(char op1, char op2) {
        if (op2 == ')' || op2 == '(') {
            return false;
        }

        if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
            return false;
        }
        return true;
    }
}
```

## 3. Time & Space Complexity

**Stack:** 时间复杂度: O(n), 空间复杂度: O(n)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/stack/772-basic-calculator-iii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
