【leetcode算法】Basic Calculator II

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid.

some examples:

 "11 + 5 * 4" =31

  "1 *8 *2 + 100/2 "=66

下面是我实现的算法,但是耗时比较长,不是太理想:

public class Solution {
    public int calculate(String value) {
          Stack<Integer> numStack = new Stack<Integer>();
        Stack<Character> symbolStack = new Stack<Character>();
        int intTmp = 0;
        for (int i = 0; i < value.length(); i++) {
            char charValue = value.charAt(i);
            int numValue = charValue - '0';
            if (charValue == 64) {
                continue;
            }
            if (isDigital(charValue)) {
                intTmp = intTmp * 10 + numValue;
                if (i < value.length() -1&&isDigital(value.charAt(i + 1))) {
                    continue;
                }
                numStack.push(intTmp);
                intTmp = 0;
                while (numStack.size() >= 2) {
                    char opt = symbolStack.peek();
                    if(opt =='*' || opt == '/'){
                        int oneValue = numStack.pop();
                        int twoValue = numStack.pop();
                        symbolStack.pop();
                        if(opt == '*'){
                          numStack.push(oneValue * twoValue);
                        }
                        if(opt == '/'){
                            numStack.push(twoValue / oneValue);
                        }
                    }
                    if(opt == '+' || opt == '-'){
                      if(checkNextOptIsFirstOpt(value,i)){
                            break;
                      }else{
                          int oneValue = numStack.pop();
                          int twoValue = numStack.pop();
                          symbolStack.pop();
                          if(opt == '+'){
                              numStack.push(oneValue + twoValue);
                          }
                          if(opt == '-'){
                              numStack.push(twoValue - oneValue);
                          }
                      }
                    }
                }


            } else {
                switch (charValue) {
                    case '+':
                        symbolStack.push('+');
                        break;
                    case '-':
                        symbolStack.push('-');
                        break;
                    case '*':
                        symbolStack.push('*');
                        break;
                    case '/':
                        symbolStack.push('/');
                        break;
                }


            }


        }


        return numStack.pop();
    }
    
     private boolean checkNextOptIsFirstOpt(String value, int currentIndex) {
        for (currentIndex++; currentIndex < value.length(); currentIndex++) {
            if (value.charAt(currentIndex) == '*' || value.charAt(currentIndex) == '/') {
                return true;
            } else if (value.charAt(currentIndex) == '+' || value.charAt(currentIndex) == '-') {
                return false;
            }
        }
        return false;
    }


    private boolean isDigital(char value) {
        int number = value - '0';
        if (number <= 9 && number >= 0) {
            return true;
        }
        return false;
    }
}



你可能感兴趣的:(LeetCode,basic,calculator)