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

# 224. 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 always valid.

Some examples:

"1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23

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

## Solution <a href="#solution" id="solution"></a>

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

        Stack<Integer> stack1 = new Stack<>();
        Stack<Character> stack2 = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char currentChar = s.charAt(i);
            if (isOperand(currentChar)) {
                if (currentChar == ')') {
                    while (stack2.peek() != '(') {
                        Character operand = stack2.pop();
                        stack1.push(operate(operand, stack1.pop(), stack1.pop()));
                    }

                    stack2.pop();
                } else if (currentChar == '+' || currentChar == '-') {
                    while (!stack2.isEmpty()) {
                        if (stack2.peek() == '(') {
                            break;
                        }

                        stack1.push(operate(stack2.pop(), stack1.pop(), stack1.pop()));
                    }

                    stack2.push(currentChar);
                } else {
                    stack2.push(currentChar);
                }

            } else if (Character.isDigit(currentChar)) {
                int endIndex = i + 1;
                while (endIndex < s.length() && Character.isDigit(s.charAt(endIndex))) {
                    endIndex++;
                }

                stack1.push(Integer.valueOf(s.substring(i, endIndex)));
                i = endIndex - 1;
            }
        }

        while (!stack2.isEmpty()) {
            stack1.push(operate(stack2.pop(), stack1.pop(), stack1.pop()));
        }

        return stack1.pop();
    }

    private int operate(Character c, int a, int b) {
        switch (c) {
            case '+':
                return a + b;
            case '-':
                return b - a;
        }

        return -1;
    }

    private boolean isOperand(char c) {
        StringBuffer sb = new StringBuffer();
        sb.append(c);
        return "()+-".contains(sb.toString());
    }
}
```


---

# 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, and the optional `goal` query parameter:

```
GET https://anand-aryan.gitbook.io/leetcode/224.-basic-calculator.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
