zoukankan      html  css  js  c++  java
  • 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:

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

    Note: Do not use the eval built-in library function.

    Credits:
    Special thanks to @ts for adding this problem and creating all test cases.

    Analysis:

    Whether we can perform a+b depends on the operator after b, so for a+b*c*d/e+f+g, we just calculate like a+(b*c*d/e)+f+g.

    Solution:

    public class Solution {
        int index;
    
        public int calculate(String s) {
            if (s.isEmpty())
                return 0;
            s = s.trim();
    
            int preValue = 0;
            char preOper = '+';
            index = 0;
            while (index < s.length()) {
                // Get next number & operator.
                int nextNum = getInt(s);
                char nextOper = getOperator(s);
                if (nextOper == 'n') {
                    preValue = operate(preValue, nextNum, preOper);
                    break;
                }
    
                // if nextOper is '+' or '-', perform operation immediately.
                if (nextOper == '*' || nextOper == '/') {
                    int nnNum = getInt(s);
                    char nnOper = getOperator(s);
                    // keep calculating until reaching '+','-', or end of string
                    while (nnOper == '*' || nnOper == '/') {
                        nextNum = operate(nextNum, nnNum, nextOper);
                        nextOper = nnOper;
                        nnNum = getInt(s);
                        nnOper = getOperator(s);
                    }
                    nextNum = operate(nextNum, nnNum, nextOper);
                    nextOper = nnOper;
                }
                preValue = operate(preValue, nextNum, preOper);
                preOper = nextOper;
            }
            return preValue;
        }
    
        public char getOperator(String s) {
            char oper = 'n';
            skipSpace(s);
            if (index < s.length()) {
                oper = s.charAt(index++);
            }
            return oper;
        }
    
        public int operate(int num1, int num2, char operator) {
            switch (operator) {
            case '+':
                return num1 + num2;
            case '-':
                return num1 - num2;
            case '*':
                return num1 * num2;
            case '/':
                return num1 / num2;
            }
            return 0;
        }
    
        public int getInt(String s) {
            skipSpace(s);
            int val = 0;
            while (index < s.length() && s.charAt(index) >= '0' && s.charAt(index) <= '9') {
                val = val * 10 + (s.charAt(index++) - '0');
            }
            return val;
        }
    
        public void skipSpace(String s) {
            while (index < s.length() && s.charAt(index) == ' ') {
                index++;
            }
        }
    }
  • 相关阅读:
    spring MVC 后台token防重复提交解决方案
    redis实现分布式锁
    java spring boot项目部署-上
    倒计数锁存器(CountDown Latch)和 CyclicBarrier(同步屏障)
    通过条件注解@Conditional细粒度的选择bean实例
    Netflix中的负载均衡策略
    C# lambda表达式参数的正确使用姿势
    RabbitMQ如何保证发送端消息的可靠投递-发生镜像队列发生故障转移时
    RabbitMQ如何保证发送端消息的可靠投递
    vue项目目录结构详解
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5824392.html
Copyright © 2011-2022 走看看