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

LeetCode-224. Basic Calculator [C++][Java]

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

LeetCode-224. Basic Calculator [C++][Java]

LeetCode-224. Basic Calculatorhttps://leetcode.com/problems/basic-calculator/

Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.

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

Example 1:

Input: s = "1 + 1"
Output: 2

Example 2:

Input: s = " 2-1 + 2 "
Output: 3

Example 3:

Input: s = "(1+(4+5+2)-3)+(6+8)"
Output: 23

Constraints:

  • 1 <= s.length <= 3 * 10^5
  • s consists of digits, '+', '-', '(', ')', and ' '.
  • s represents a valid expression.
  • '+' is not used as a unary operation (i.e., "+1" and "+(2 + 3)" is invalid).
  • '-' could be used as a unary operation (i.e., "-1" and "-(2 + 3)" is valid).
  • There will be no two consecutive operators in the input.
  • Every number and running calculation will fit in a signed 32-bit integer.

【C++】
class Solution {
public:
    int calculate(string s) {
        int res = 0, num = 0, sign = 1;
        stack st;
        for (auto c : s ) {
            if (isdigit(c)) {num = num*10 + (c-'0');}
            else if(c == '+') {
                res += num*sign;
                sign = 1;
                num = 0;
            } else if(c == '-') {
                res += num*sign;
                sign = -1;
                num = 0;
            } else if(c == '(') {
                st.push(res);
                st.push(sign);
                res = 0;
                sign = 1;
                num = 0;
            } else if(c == ')') {
                res += sign*num;
                num = 0;
                sign = 1;
                res *= st.top(); st.pop();
                res += st.top(); st.pop();
            }
        }
        return res + sign*num;
    }
};

【Java】
class Solution {
    public int calculate(String s) {
        int res = 0, num = 0, sign = 1;
        Stack st = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {num = num*10 + (c-'0');}
            else if(c == '+') {
                res += num*sign;
                sign = 1;
                num = 0;
            } else if(c == '-') {
                res += num*sign;
                sign = -1;
                num = 0;
            } else if(c == '(') {
                st.push(res);
                st.push(sign);
                res = 0;
                sign = 1;
                num = 0;
            } else if(c == ')') {
                res += sign*num;
                num = 0;
                sign = 1;
                res *= st.pop();
                res += st.pop();
            }
        }
        return res + sign*num;
    }
}

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

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

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