【LeetCode】224. Basic Calculator 基本计算器(Hard)(JAVA)

【LeetCode】224. Basic Calculator 基本计算器(Hard)(JAVA)

题目地址: https://leetcode.com/problems/basic-calculator/

题目描述:

mplement a basic calculator to evaluate a simple expression string.

The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .

Example 1:

Input: "1 + 1"
Output: 2

Example 2:

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

Example 3:

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

Note:

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

题目大意

实现一个基本的计算器来计算一个简单的字符串表达式的值。

字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格 。

解题方法

  1. 遇到数字,就求出当前的完整 Integer ,然后放到 list 里面
  2. 遇到 ‘+’ 或 ‘-’,如果下一个是 ‘(’ 左括号,说明下面的计算是优先的,所以把符号存到一个 operate 列表里;如果没有遇到做空括号,直接拿出 list 的最后一个数字和下一个数字进行计算
  3. 遇到 ‘(’ 左括号,除了开头的左括号,后面不可能遇到左括号了(因为第二步已经处理了左括号的情况),所以直接继续就行,开头的左括号没有意义
  4. 遇到 ‘)’ 右括号,说明当前的优先计算结束了,那就把 list 中的最后两个值计算,具体怎么计算?拿出 operate 存的符号进行计算就行
  5. 最后的结果就是 list 的第一个
class Solution {
    public int calculate(String s) {
        List<Integer> list = new ArrayList<>();
        List<Character> operate = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            char temp = s.charAt(i);
            if (temp == ' ') continue;
            if (temp == '+' || temp == '-') {
                int[] next = getNext(s, i + 1);
                if (next[1] < s.length() && s.charAt(next[1]) == '(') {
                    operate.add(temp);
                    i = next[1];
                } else {
                    Integer pre = list.remove(list.size() - 1);
                    list.add(pre + next[0] * (temp == '+' ? 1 : -1));
                    i = next[1] - 1;
                }
            } else if (temp == '(') {
                continue;
            } else if (temp == ')') {
                if (operate.size() > 0) {
                    Character ch = operate.remove(operate.size() - 1);
                    Integer first = list.remove(list.size() - 1);
                    Integer second = list.remove(list.size() - 1);
                    list.add(second + first * (ch == '+' ? 1 : -1));
                }
            } else {
               int[] next = getNext(s, i);
               list.add(next[0]);
               i = next[1] - 1;
            }
        }
        return list.get(0);
    }

    public int[] getNext(String s, int start) {
        int res = 0;
        char ch;
        while (start < s.length() && s.charAt(start) == ' ') {
            start++;
        }
        while (start < s.length() && (ch = s.charAt(start)) >= '0' && ch <= '9') {
            res = res * 10 + ch - '0';
            start++;
        }
        while (start < s.length() && s.charAt(start) == ' ') {
            start++;
        }
        return new int[]{res, start};
    }
}

执行耗时:25 ms,击败了37.25% 的Java用户
内存消耗:39.4 MB,击败了50.29% 的Java用户

欢迎关注我的公众号,LeetCode 每日一题更新
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值