栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

LeetCode-227. Basic Calculator II [C++][Java]

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode-227. Basic Calculator II [C++][Java]

LeetCode-227. Basic Calculator II https://leetcode.com/problems/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 [-231, 231 - 1].

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 * 10^5
  • 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].
  • The answer is guaranteed to fit in a 32-bit integer.

【C++】
class Solution {
public:
    int calculate(string s) {
        int i = 0;
        return parseExpr(s, i);
    }

    // 辅函数- 递归parse从位置i开始的剩余字符串
    int parseExpr(const string& s, int& i) {
        char op = '+';
        long left = 0, right = 0;
        while (i < s.length()) {
            if (s[i] != ' ') {
                long n = parseNum(s, i);
                switch (op) {
                    case '+' : left += right; right = n; break;
                    case '-' : left += right; right = -n; break;
                    case '*' : right *= n; break;
                    case '/' : right /= n; break;
                }
                if (i < s.length()) {op = s[i];}
            }
            ++i;
        }
        return left + right;
    }

    // 辅函数- parse从位置i开始的一个数字
    long parseNum(const string& s, int& i) {
        long n = 0;
        while (i < s.length() && isdigit(s[i])) {
            n = 10 * n + (s[i++] - '0');
        }
        return n;
    }
};

【Java】
class Solution {
    public int calculate(String s) {
        int i = 0;
        return parseExpr(s, i);
    }

    // 辅函数- 递归parse从位置i开始的剩余字符串
    int parseExpr(String s, int i) {
        char op = '+';
        long left = 0, right = 0;
        while (i < s.length()) {
            if (s.charAt(i) != ' ') {
                long n = parseNum(s, i);
                switch (op) {
                    case '+' : left += right; right = n; break;
                    case '-' : left += right; right = -n; break;
                    case '*' : right *= n; break;
                    case '/' : right /= n; break;
                }
                if (i < s.length()) {op = s.charAt(i);}
            }
            ++i;
        }
        long res = left + right;
        return (int)res;
    }

    // 辅函数- parse从位置i开始的一个数字
    long parseNum(String s, int i) {
        long n = 0;
        while (i < s.length() && Character.isDigit(s.charAt(i))) {
            n = 10 * n + (s.charAt(i++) - '0');
        }
        return n;
    }
}

参考文献

【1】Java中long(Long)与int(Integer)之间的转换

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/861871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号