LeetCode-Basic Calculator

Posted LiBlog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Basic Calculator相关的知识,希望对你有一定的参考价值。

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++;
        }
    }
}

 

以上是关于LeetCode-Basic Calculator的主要内容,如果未能解决你的问题,请参考以下文章

leetcode-Basic Calculator-224

Arcgis中用raster calculator怎样 计算山的顶点,应怎样编写

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:Calculator.main 中的 0(Calculator.java:25)[重复]

typescript 开发人员日记:第1天 - packages / server / src / calculator / calculator.ts

LeetcodeBasic Calculator

LeetcodeBasic Calculator II