LeetCode //C - 227. Basic Calculator II

227. Basic Calculator II

Given a string s which represents an expression, evaluate this expression and return its value.

The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [ − 2 31 , 2 31 − 1 ] [-2^{31}, 2^{31} - 1] [231,2311].

Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
 

Example 1:

Input: s = “3+2*2”
Output: 7

Example 2:

Input: s = " 3/2 "
Output: 1

Example 3:

Input: s = " 3+5 / 2 "
Output: 5

Constraints:
  • 1 <= s.length <= 3 * 1 0 5 10^5 105
  • s consists of integers and operators (‘+’, ‘-’, ‘*’, ‘/’) separated by some number of spaces.
  • s represents a valid expression.
  • All the integers in the expression are non-negative integers in the range [ 0 , 2 31 − 1 ] [0, 2^{31} - 1] [0,2311].
  • The answer is guaranteed to fit in a 32-bit integer.

From: LeetCode
Link: 227. Basic Calculator II


Solution:

Ideas:
  • Stack Initialization: int stack[300000]; - We use a large array to simulate the stack.
  • Variables: int top = -1; - This variable keeps track of the top of the stack. int currentNumber = 0; - This stores the current number being processed.
  • Iteration through string: The loop iterates through each character in the string.
    • If a digit is found, it builds the currentNumber.
    • If a non-digit or end of string is found, the code processes the previous number with the current operation.
  • Operations Handling:
    • Based on the operation, the code either pushes the number onto the stack or performs multiplication/division and updates the stack.
  • Final Calculation: The loop sums all the values in the stack to get the final result.
Code:
int calculate(char* s) {
    int stack[300000];
    int top = -1;
    int currentNumber = 0;
    char operation = '+';
    
    for (int i = 0; s[i] != '\0'; i++) {
        if (isdigit(s[i])) {
            currentNumber = currentNumber * 10 + (s[i] - '0');
        }
        if (!isdigit(s[i]) && !isspace(s[i]) || s[i + 1] == '\0') {
            if (operation == '+') {
                stack[++top] = currentNumber;
            } else if (operation == '-') {
                stack[++top] = -currentNumber;
            } else if (operation == '*') {
                stack[top] *= currentNumber;
            } else if (operation == '/') {
                stack[top] /= currentNumber;
            }
            operation = s[i];
            currentNumber = 0;
        }
    }
    
    int result = 0;
    for (int i = 0; i <= top; i++) {
        result += stack[i];
    }
    
    return result;
}

相关推荐

  1. LeetCode 227. 基本计算器 II

    2024-07-16 20:16:05       24 阅读
  2. 207. 课程表

    2024-07-16 20:16:05       59 阅读
  3. 栈-227.基本计算器II(四则运算)

    2024-07-16 20:16:05       28 阅读
  4. LeetCode //C - 227. Basic Calculator II

    2024-07-16 20:16:05       25 阅读
  5. 207. Course Schedule

    2024-07-16 20:16:05       67 阅读
  6. LeetCode 224:基本计算器

    2024-07-16 20:16:05       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 20:16:05       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 20:16:05       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 20:16:05       62 阅读
  4. Python语言-面向对象

    2024-07-16 20:16:05       72 阅读

热门阅读

  1. 什么样的服务器是合乎直销网站标准

    2024-07-16 20:16:05       18 阅读
  2. rabbitmq消息投递失败

    2024-07-16 20:16:05       20 阅读
  3. 网络通信介绍

    2024-07-16 20:16:05       18 阅读
  4. decimal.js库

    2024-07-16 20:16:05       21 阅读
  5. 自我承诺闭环

    2024-07-16 20:16:05       18 阅读
  6. 通讯录-C/C++

    2024-07-16 20:16:05       20 阅读
  7. Docker 三剑客

    2024-07-16 20:16:05       23 阅读
  8. Spring注解的实现原理【简单实现一个注解】

    2024-07-16 20:16:05       20 阅读
  9. 洛谷 P10119 题解

    2024-07-16 20:16:05       20 阅读
  10. 初识C++

    初识C++

    2024-07-16 20:16:05      18 阅读
  11. 删除文件夹下的文件

    2024-07-16 20:16:05       20 阅读
  12. Vue3.0中实现的动态路由权限控制

    2024-07-16 20:16:05       21 阅读