[string]Basic Calculator II

Total Accepted: 14291 Total Submissions: 64507 Difficulty: Medium

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.

(M) Basic Calculator (H) Expression Add Operators

对表达式按+-划分,含乘除的表达式部分当做一个整体,如果当前运算符是+-号,说明前一个表达的结果已经计算完成,那么把前一个表达式的结果加到输出结果中,如果是*/则说明表达式尚未结束。

/*
题目已知:
1.只包含非负整数,加减乘除,空格
2.假设输入一直合法
涉及的几个点:
1.数字分割
2.字符串转整数
3.运算符的优先级
可能隐藏的点:
大数
测试案例:
"0"
"1+0"
"1+10"
" 13 + 24 "
" 23+ 34*3 /2 "
" 12 - 7*3/2 + 35/7-3 "
" 7*2/3 + 9"
"12 - 7*3/2 + 35/7"
*/
class Solution {
public:
    int calculate(string s) {
        int size = s.size();
        long long int exp_res = 0,res=0;
        long int num =0;
        bool has_pre_op = false;
        char pre_op;
        for(int i=0;i<size;i++){
            if(s[i]==‘ ‘) continue;
            if(isdigit(s[i])){
                num = num*10+(s[i]-‘0‘);
                if(i+1 == size || !isdigit(s[i+1])){//如果下一个位置是最后一个位置,或者下一个位置不是数字了
                    if(has_pre_op){//当前运算数之前有操作符
                        if(pre_op==‘+‘){
                            exp_res = num;
                        }else if(pre_op==‘-‘){
                            exp_res = -num;
                        }else if(pre_op==‘*‘){
                            exp_res *= num;
                        }else{
                            exp_res /= num;
                        }
                    }else{
                        exp_res = num;
                    }
                }
            }else{
                if(s[i]==‘+‘ || s[i]==‘-‘){
                    res += exp_res;
                }
                pre_op = s[i];
                has_pre_op = true;
                num=0;
            }
        }
        return res+exp_res;
    }
};

Next challenges: (M) Basic Calculator (H) Expression Add Operators

时间: 2024-12-21 16:18:13

[string]Basic Calculator II的相关文章

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

【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 227. 基本计算器 II(Basic Calculator II)

227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和空格.整数除法仅保留整数部分. LeetCode227. Basic Calculator II中等 示例?1: 输入: "3+2*2" 输出: 7 示例 2: 输入: " 3/2 " 输出: 1 示例 3: 输入: " 3+5 / 2 " 输出:

LeetCode——Basic Calculator II

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

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

[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

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] 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. You may assume that the give