Algorithmic Clearance Village - Analysis of Basic Calculator Problems

227. Basic Calculator II

Basic Calculator
Given a string expression s, please implement a basic calculator to calculate and return its value.

Integer division keeps only the integer part.

You can assume that a given expression is always valid. All intermediate results will be in the range [-231, 231 - 1].

Note: Any built-in functions that evaluate strings as mathematical expressions, such as eval(), are not allowed.

Example 1:

Input: s = "3+2*2"
Output: 7

the stack

First of all, the string must be disassembled, and the number and addition, subtraction, multiplication and division must be judged. The number needs to be converted from a character to a number. Then, when addition is encountered, the current num is added to the stack. Subtraction is the opposite number of the current number. , both multiplication and division need to take out the top element of the stack and then process it. Finally, the stack sum needs to be calculated.

step:

  1. iterate over the string
  2. judge numbers, convert
  3. Addition, add the number num, push to the stack
  4. Subtraction, add the opposite number of numbers, push to the stack
  5. Multiplication, the top element of the stack is multiplied by the number, and the stack is pushed
  6. Division, the top element of the stack is divided by the number, and the stack is pushed
  7. 栈和和
 public int calculate(String s) {
    
    
        Stack<Integer> stack = new Stack<>();
        // 当前num值
        int num = 0;
        // 默认选择
        char preSign = '+';
        for(int i=0;i<s.length();i++){
    
    
            // 判断字符是否是数字
        if(Character.isDigit(s.charAt(i))){
    
    
            // 转换为整数,多位字符进行拼接
            num = num * 10 +s.charAt(i)-'0';
        }
            // 不是数字,空格,不是最后一个字符
        if(!Character.isDigit(s.charAt(i)) && s.charAt(i) !=' ' || i==s.length()-1){
    
    
            switch(preSign){
    
    
                case '+':
                    // 添加
                    stack.push(num);
                    break;
                case '-':
                    // 添加负数
                     stack.push(-num);
                     break;
                case '*':
                    // 乘号去除栈顶元素和当前num相乘
                    stack.push(stack.pop()*num);
                    break;
                case '/':  
                    // 栈顶元素和当前num相除
                    stack.push(stack.pop()/num);             

            }
            // 获取当前的字符,更新
            preSign = s.charAt(i);
            // num归0,重置
            num=0;
        }
        }
        // 计算之和
        int ans = 0;
        while(!stack.isEmpty()){
    
    
            ans += stack.pop(); 
        }

        return ans;
    }

Time complexity: O(n) traverses the string once, and then judge
Space complexity: O(n) The elements in the stack are taken out and added

Summary: This question did not consider the multi-digit situation at the beginning, and the method of judging whether the corresponding character is a number does not know how to judge.

Guess you like

Origin blog.csdn.net/qq_52843958/article/details/132008118