BiruLyu
8/5/2017 - 4:52 AM

227. Basic Calculator II(#two stack).java

public class Solution {
    public int calculate(String s) {
        s = s + "+";
        char[] expr = s.toCharArray();
        int res = 0, multiend = 0;
        int curNum = 0;
        char lastSign = '+';
        for(char c: expr) {
            if(c == ' ')
                continue;
            if(c >= '0' && c <= '9')
                curNum = curNum*10+c-'0';
            else{
                if(lastSign == '+') {
                    res += curNum;
                    multiend = curNum;
                }else if(lastSign == '-') {
                    res -= curNum;
                    multiend = -curNum;
                }else if(lastSign == '*') {
                    res = res-multiend + multiend*curNum;
                    multiend = multiend*curNum;
                }else{
                    res = res-multiend + multiend/curNum;
                    multiend = multiend/curNum;
                }
                curNum = 0;
                lastSign = c;
            }
        }
        return res;
    }
}
public class Solution {
public int calculate(String s) {
    int len;
    if(s==null || (len = s.length())==0) return 0;
    Stack<Integer> stack = new Stack<Integer>();
    int num = 0;
    char sign = '+';
    for(int i=0;i<len;i++){
        if(Character.isDigit(s.charAt(i))){
            num = num*10+s.charAt(i)-'0';
        }
        if((!Character.isDigit(s.charAt(i)) &&' '!=s.charAt(i)) || i==len-1){
            if(sign=='-'){
                stack.push(-num);
            }
            if(sign=='+'){
                stack.push(num);
            }
            if(sign=='*'){
                stack.push(stack.pop()*num);
            }
            if(sign=='/'){
                stack.push(stack.pop()/num);
            }
            sign = s.charAt(i);
            num = 0;
        }
    }

    int re = 0;
    for(int i:stack){
        re += i;
    }
    return re;
}
public class Solution {
    private int getResult(int a, int b, char op) {
        switch(op) {
            case '+' : return a + b;
            case '-' : return a - b;
            case '*' : return a * b;
            default: return a / b;
        }
    }
    public int calculate(String s) {
        if (s == null || s.length() < 1) return 0;
        int len = s.length();
        int i = 0;
        int num = 0; 
        Stack<Character> operators = new Stack<>();
        Stack<Integer> operands = new Stack<>();
        char[] str = s.toCharArray();
        while (i < len) {
            char c = str[i];
            if (c == ' ') {
                i++;
                continue;
            } else if (c >= '0' && c <='9') {
                num = c - '0';
                i++;
                while (i < len && str[i] >= '0' && str[i] <= '9') {
                    num = num * 10 + str[i] - '0';
                    i++;
                }
                operands.push(num);
                num = 0;
            } else if (c == '*' || c == '/'  ) {
                while (!operators.isEmpty() && (operators.peek() != '+' && operators.peek() != '-')) {  // "100000000/1/2/3/4/5/6/7/8/9/10"
                    int second = operands.pop();
                    int first = operands.pop();
                    operands.push(getResult(first, second, operators.pop()));
                }
                operators.push(c);
                i++;
            } else if (c == '+' || c == '-') {
                while (!operators.isEmpty()) {
                    int second = operands.pop();
                    int first = operands.pop();
                    operands.push(getResult(first, second, operators.pop()));
                }
                operators.push(c);
                i++;
            }        
        }               
              
        while (!operators.isEmpty()) {
            int second = operands.pop();
            int first = operands.pop();
            operands.push(getResult(first, second, operators.pop()));
        }
        return operands.pop();
    }
}