227. Basic Calculator II**

227. 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 should truncate toward zero.

Example 1:

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

Example 2:

Input: " 3/2 "
Output: 1

Example 3:

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

Note:

  • You may assume that the given expression is always valid.
  • Do not use the eval built-in library function.

C++ 实现 1

原来写计算器也不是那么容易的…

完成本题参考了:

我最后的解法和他们的不同, 原因在于我想按照自己的方式实现一下. 最后发现实现的代码和他们的不同. C++ 实现 1 先介绍我的思路.

class Solution {
public:
    int calculate(string s) {
        // 使用 op 记录当前遇到的符号, 用 prev 记录上一个数, 用 num 记录当前遇到的数
        s = s + "++";
        long long prev = 0, num = 0, total = 0;
        char op = '\0';
        for (int i = 0; i < s.size(); ++ i) {
            // cout << num << endl;
            if (std::isdigit(s[i])) num = num * 10 + s[i] - '0';
            else if (!std::isdigit(s[i]) && !std::isspace(s[i])) {
                if (op == '\0') prev = num;
                if (op == '+' || op == '-') {
                    total += prev;
                    num *= op == '-' ? -1 : 1;
                    prev = num;
                } else if (op == '*') prev *= num;
                else if (op == '/') prev /= num;
                op = s[i];
                num = 0;
            }
        }
        return total;
    }
};

C++ 实现 2

来自: 17 lines C++, easy, 20 ms, 我做了一点修改. 代码很好理解, 不多介绍.

class Solution {
public:
    int calculate(string s) {
        istringstream in('+' + s + '+');
        long long total = 0, term = 0, n;
        char op;
        while (in >> op) {
            if (op == '+' or op == '-') {
                total += term;
                in >> term;
                term *= op == '-' ? -1 : 1;
            } else {
                in >> n;
                if (op == '*')
                    term *= n;
                else
                    term /= n;
            }
        }
        return total;
    }
};

C++ 实现 3

参考: Share my java solution, 修改为 C++;

class Solution {
public:
    int calculate(string s) {
        stack<long long> st;
        long long num = 0;
        char sign = '+';
        for (int i = 0; i < s.size(); ++ i) {
            if (std::isdigit(s[i])) num = num * 10 + s[i] - '0';
            if ((!std::isdigit(s[i]) && !std::isspace(s[i])) || (i == s.size() - 1)) {
                if (sign == '-') st.push(-num);
                if (sign=='+') st.push(num);
                if (sign=='*'){
                    auto val = st.top();
                    st.pop();
                    st.push(val * num);
                }
                if (sign=='/'){
                    auto val = st.top();
                    st.pop();
                    st.push(val / num);
                }
                sign = s[i];
                num = 0;
            }
        }

        long long res = 0;
        while (!st.empty()) {
            res += st.top();
            st.pop();
        }
        return res;
    }
};
发布了327 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Eric_1993/article/details/104445342
今日推荐