Leetcode 227. Basic Calculator II JAVA语言

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.

题意:求值

public class Solution {
    public int calculate(String s) {
        int digt=0;
        char op=‘+‘;
        int res=0;
        int n=s.length();
        Stack<Integer> stack=new Stack<>();
        for(int i=0;i<n;i++){
            if(s.charAt(i)>=‘0‘ && s.charAt(i)<=‘9‘)
                digt=digt*10+s.charAt(i)-‘0‘;
                int temp=digt;
            if(s.charAt(i)==‘+‘ || s.charAt(i)==‘-‘ || s.charAt(i)==‘*‘ || s.charAt(i)==‘/‘ || i==n-1){
                switch(op){
                    case ‘+‘:stack.push(digt);break;
                    case ‘-‘:stack.push(-digt);break;
                    case ‘*‘:stack.push(stack.pop()*digt);break;
                    case ‘/‘:stack.push(stack.pop()/digt);break;
                }     
                op=s.charAt(i);
                digt=0;
            }
        }
        while(!stack.empty()){
            res+=stack.pop();
        }
        return res;
    }
}

PS:

Stack

  1. 定义一个temp 的stack来保存运算的操作数(包括中间的运算值)
  2. d 来保存上一个操作数
  3. op 来保存上一个操作数,不是当前的操作数
  4. 遍历字符串
    1. 如果是数字,入 stack
    2. 如果是操作符
      1. +: 将 +d 存入stack
      2. -: 将 -d 存入 stack
      3. * : 将 stack 的top值与 d 的乘积存入 stack
      4. /: 将 stack 的top 值与 d 的除值 存入 stack

空间复杂度是 O(N), 时间复杂度是 O(N)。需要注意的是:每次操作数入栈的时间是下一个操作符的时候,有点类似与延迟运算,所以需要把最近的操作数和操作符存入两个额外的变量。还有一个小技巧,如果我们给字符串的前面和后面分别增加一个‘+‘操作符,可以减少代码的复杂度。如 "1+2*3" 修改成 "+1+2*3+ "。

时间: 2024-10-07 06:00:28

Leetcode 227. Basic Calculator II JAVA语言的相关文章

leetcode 227. Basic Calculator II ---------- java

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 ex

Java for LeetCode 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 ex

[LeetCode#227] Basic Calculator II

Problem: 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

【LeetCode】227. Basic Calculator II

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 assu

LeetCode OJ Basic Calculator II

Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: string trans_to_postfix_expression_to_s(string); // 将得到的表达式转化为后缀表达式 long long int calculate_from_postfix_expression(); // 根据后缀表达式计算值 private: vector<string

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 give

【Leetcode】Basic Calculator II

题目链接:https://leetcode.com/problems/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 s

[leedcode 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 ex

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. Example 1: Input: "3+2*2&q