java 227.基本计算器II(#two stack).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 227.基本计算器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();
    }
}

以上是关于java 227.基本计算器II(#two stack).java的主要内容,如果未能解决你的问题,请参考以下文章

java 227.基本计算器II(#two stack).java

java 227.基本计算器II(#two stack).java

java 227.基本计算器II(#two stack).java

java 227.基本计算器II(#two stack).java

LeetCode 227. 基本计算器 II c++/java详细题解

LeetCode 227. 基本计算器 II(Basic Calculator II)