Saturday, August 19, 2017

224. Basic Calculator

{2ND TRY}
Bug Edge Case
"1-(5)"

38.38 %
class Solution {
    public int calculate(String s) {
if (s == null || s.length() == 0) {
return 0;
}
Deque<Object> stack = new ArrayDeque<>();
int i = 0;
int sum = 0;
while (i < s.length()) {
char curr = s.charAt(i);
if (Character.isDigit(curr)) {
int num = curr - '0';
while (i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))) {
num = num * 10 + Integer.valueOf(s.charAt(i + 1) - '0');
i++;
}
if (stack.peekFirst() instanceof Character && (char)stack.peekFirst() == '-') {
stack.removeFirst();
num = -num;
}
stack.addFirst(num);
} else if (curr == '(' || curr == '-') {
stack.addFirst(curr);
} else if (curr == ')') {
sum = 0;
while (!(stack.peekFirst() instanceof Character)) {
sum += (int)stack.removeFirst();
}
if (stack.peekFirst() instanceof Character && (char)stack.peekFirst() == '(') {
stack.removeFirst();
if (stack.peekFirst() instanceof Character && (char)stack.peekFirst() == '-') {
stack.removeFirst();
sum = -sum;
}
stack.push(sum);
}
}
i++;
}
sum = 0;
while (!stack.isEmpty()) {
sum += (int)stack.removeFirst();
}
return sum;
}
}


{1ST TRY}
"Simple iterative solution by identifying characters one by one. One important thing is that the input is valid, which means the parentheses are always paired and in order.
Only 5 possible input we need to pay attention:
  1. digit: it should be one digit from the current number
  2. '+': number is over, we can add the previous number and start a new number
  3. '-': same as above
  4. '(': push the previous result and the sign into the stack, set result to 0, just calculate the new result within the parenthesis.
  5. ')': pop out the top two numbers from stack, first one is the sign before this pair of parenthesis, second is the temporary result before this pair of parenthesis. We add them together.
Finally if there is only one number, from the above solution, we haven't add the number to the result, so we do a check see if the number is zero.
"

84.00 %

class Solution {
    // "(1+(4+5+2)-3)+6+8" = 23
    public int calculate(String s) {
        if (s == null) return 0;
        char[] chars = s.trim().toCharArray();
        Stack<Integer> stack = new Stack<>();
        int num = 0;
        int result = 0;
        int sign = 1;
        for (char c : chars) {
            if (c == '(') {
                stack.push(result);
                stack.push(sign);
                num = 0;
                result = 0;
                sign = 1;
            } else if (Character.isDigit(c)) {
                num = num * 10 + Character.getNumericValue(c);
            } else if (c == '+') { // Digits are over
                result += sign * num;
                sign = 1;
                num = 0;
            } else if (c == '-') {
                result += sign * num;
                sign = -1;
                num = 0;
            } else if (c == ')') {
                result += sign * num;
                sign = stack.pop();
                num = stack.pop();
                result = num + sign * result;
                num = 0;
            } else {
                continue;
            }
        }
        if (num != 0) {
            result += sign * num;
        }
        return result;
    }
}





No comments:

Post a Comment