public int calculate(String s) {
        int res = 0;
        Stack<Integer> sign = new Stack<Integer>();
        sign.push(1); sign.push(1);
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            if (ch == ' ') {
                continue;
            } else if (ch >= '0') {
                int j = i;
                int num = 0;
                while (j < s.length() && s.charAt(j) >= '0') {
                    num = num * 10 + (s.charAt(j) - '0');
                    ++j;
                }
                res += sign.pop() * num;
                i = j - 1;
            } else if (ch == '(') {
                sign.push(sign.peek());
            } else if (ch == '+') {
                sign.push(sign.peek());
            } else if (ch == '-') {
                sign.push(-1 * sign.peek());
            } else if (ch == ')') {
                sign.pop();
            }
        }
        return res;
    }