FB计算器加乘括号

加乘无括号

public static int simpleCaculator(String s){
        if (s == null || s.length() == 0){
            return 0;
        }
        int res = 0;
        int num = 0;
        char sign = '+';
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if (Character.isDigit(c)){
                num = c - '0';
                while (i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))){
                    num = num*10 + s.charAt(i+1) - '0';
                    i++;
                }
            }
            if (!Character.isDigit(c) && c != ' ' || i == s.length() - 1){
                if (sign == '+'){
                    stack.push(num);
                } else if (sign == '*'){
                    stack.push(stack.pop() * num);
                }
                sign = c;
                num = 0;
            }
        }
        for (int i : stack){
            res += i;
        }
        return res;
    }

加乘带括号
用两个stack来做,stack1用来存num跟(, stack2用来存(之前的sign. 注意到这里我们用Long.MAX_VALUE来代表(, 这也是为什么我们选择stack of long, 因为如果选择stack of integer的话,我们允许的数据范围也可以取到Integer.MAX_VALUE, 就不能找到数字来代表左括弧了.

class Solution {
    public int calculate(String s) {
        if (s == null || s.length() == 0) {
                return 0;
        }
        char sign = '+';
        Stack<Long> stack1 = new Stack<>(); // store digit and '('
        Stack<Character> stack2 = new Stack<>(); // store sign before '('
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isDigit(ch)) {
                long num = 0;
                while (i < s.length() && Character.isDigit(s.charAt(i))) {
                    num = num * 10 + s.charAt(i++) - '0';
                }
                i--;
                stack1.push(eval(sign, stack1, num));
            } else if (ch == ' ') {
                continue;
            } else if (ch == '(') {
                stack1.push(Long.MAX_VALUE);
                stack2.push(sign);
                sign = '+';
            } else if (ch == ')') {
                long num = 0;
                while (stack1.peek() != Long.MAX_VALUE) {
                    num += stack1.pop();
                }
                stack1.pop(); // pop out '(' (Long.MAX_VALUE)
                char operator = stack2.pop();
                stack1.push(eval(operator, stack1, num));
            } else {
                sign = ch;
            }
        }
        // what we need to do is just sum up all num in stack
        int result = 0;
        while (!stack1.isEmpty()) {
            result += stack1.pop();
        }
        return result;
    }
    
    private Long eval(char sign, Stack<Long> stack1, long num) {
        if (sign == '+') {
            return num;
        } else if (sign == '-') {
            return -num;
        } else if (sign == '*') {
            return stack1.pop() * num;
        } else {
            return stack1.pop() / num;
        }
    }
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/c9b1de4417f1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞