• 227. 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:

    "3+2*2" = 7
    " 3/2 " = 1
    " 3+5 / 2 " = 5
    

    fb: 

    例如,Input是:13*(1256*31-(33/2))-5/8+9,然后output是计算出来的integer结果

    using two stacks,  one for operands and one for operators.

     

     

    public int calculate(String s) {
        if (s == null) return 0;
        s = s.trim().replaceAll(" +", "");
        int length = s.length();
        
        int res = 0;
        long preVal = 0; // initial preVal is 0
        char sign = '+'; // initial sign is +
        int i = 0;
        while (i < length) {
            long curVal = 0;
            while (i < length && (int)s.charAt(i) <= 57 && (int)s.charAt(i) >= 48) { // int
                curVal = curVal*10 + (s.charAt(i) - '0');
                i++;
            }
            if (sign == '+') {
                res += preVal;  // update res
                preVal = curVal;
            } else if (sign == '-') {
                res += preVal;  // update res
                preVal = -curVal;
            } else if (sign == '*') {
                preVal = preVal * curVal; // not update res, combine preVal & curVal and keep loop
            } else if (sign == '/') {
                preVal = preVal / curVal; // not update res, combine preVal & curVal and keep loop
            }
            if (i < length) { // getting new sign
                sign = s.charAt(i);
                i++;
            }
        }
        res += preVal;
        return res;
    }
    
     public int calculate(String s) {
            if (s == null) {
                return 0;
            }    
            Queue<Character> q = new LinkedList<>();
            for (char c : s.toCharArray()) {
                q.offer(c);
            }
            q.offer('+');
            return cal(q);
        }
        
        private int cal(Queue<Character> q) {
            char sign = '+';
            int num = 0;
            Stack<Integer> stack = new Stack<>();
            while (!q.isEmpty()) {
                char c = q.poll();
                if (c == ' ') {
                    continue;
                }
                if (Character.isDigit(c)) {
                    num = 10 * num + c - '0';
                } else if (c == '(') {
                    num = cal(q);
                } else {
                    if (sign == '+') {
                        stack.push(num);
                    } else if (sign == '-') {
                        stack.push(-num);
                    } else if (sign == '*') {
                        stack.push(stack.pop() * num);
                    } else if (sign == '/') {
                        stack.push(stack.pop() / num);
                    }
                    num = 0;
                    sign = c;
                    if (c == ')') {
                        break;
                    }
                }
            }
            int sum = 0;
            while (!stack.isEmpty()) {
                sum += stack.pop();
            }
            return sum;
        }
    

      

      

  • 相关阅读:
    部分页面开启宽屏模式
    门户diy实现翻页功能的方法
    git命令详解,从入门到装逼
    array方法常用记载
    vue 生命周期的理解(created && mouted的区别)
    微信小程序传值的几种方式
    data-*
    本地存储和会话存储以及cookie的处理
    vue的安装和项目构建
    进击的UI----------动画
  • 原文地址:https://www.cnblogs.com/apanda009/p/8133443.html
Copyright © 2020-2023  润新知