LeetCode 772: Basic Calculator III Solution

Master LeetCode problem 772 (Basic Calculator III), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

772. Basic Calculator III

Problem Explanation

Explanation:

This problem can be solved using a stack to keep track of the operands and operators while iterating through the input expression. We will handle different cases based on the operators encountered - +, -, *, /, and parentheses (, ).

  1. Iterate through the expression character by character.
  2. If the character is a digit, build the operand until the next character is not a digit.
  3. If the character is an operator or parenthesis:
    • For + and -, we directly push the current operand and operator to the stack.
    • For * and /, we pop the last operand from the stack, perform the operation, and push the result back to the stack.
    • For (, we push the current result and operator to the stack and reset the current result.
    • For ), we calculate the result until we encounter the corresponding (.
  4. Finally, sum up all the elements in the stack to get the final result.

Time Complexity: O(n), where n is the length of the input expression. Space Complexity: O(n) for the stack.

:

Solution Code

class Solution {
    public int calculate(String s) {
        Stack<Integer> stack = new Stack<>();
        char sign = '+';
        int num = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                num = num * 10 + (c - '0');
            }
            if ((!Character.isDigit(c) && c != ' ') || i == s.length() - 1) {
                if (sign == '+') {
                    stack.push(num);
                } else if (sign == '-') {
                    stack.push(-num);
                } else if (sign == '*') {
                    stack.push(stack.pop() * num);
                } else if (sign == '/') {
                    stack.push(stack.pop() / num);
                }
                sign = c;
                num = 0;
            }
        }
        
        int result = 0;
        while (!stack.isEmpty()) {
            result += stack.pop();
        }
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 772 (Basic Calculator III)?

This page provides optimized solutions for LeetCode problem 772 (Basic Calculator III) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 772 (Basic Calculator III)?

The time complexity for LeetCode 772 (Basic Calculator III) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 772 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 772 in Java, C++, or Python.

Back to LeetCode Solutions