当前位置: 代码网 > it编程>软件设计>算法 > LeetCode //C - 227. Basic Calculator II

LeetCode //C - 227. Basic Calculator II

2024年08月02日 算法 我要评论
【代码】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:
example 2:
example 3:
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;
}
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com