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++;
            }
        }
    }
  • 相关阅读:
    bzoj1297: [SCOI2009]迷路
    bzoj1875: [SDOI2009]HH去散步
    bzoj2466: [中山市选2009]树
    bzoj1770: [Usaco2009 Nov]lights 燈
    BZOJ 1965: [Ahoi2005]SHUFFLE 洗牌( 数论 )
    BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
    BZOJ 1006: [HNOI2008]神奇的国度( MCS )
    BZOJ 1925: [Sdoi2010]地精部落( dp )
    BestCoder Round #57 (div.2)
    BZOJ 1216: [HNOI2003]操作系统( 优先队列 )
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5824392.html
Copyright © 2011-2022 走看看