227. 基本计算器 II

给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。

整数除法仅保留整数部分。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/basic-calculator-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

简易版

import java.util.Scanner;
import java.util.Stack;
import java.util.stream.Collectors;

class Solution {

    public int calculate(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        char sign = '+';
        Stack<Integer> stack = new Stack<>();
        int num = 0;
        for (int i = 0; i < s.length(); ++i) {
            if (Character.isDigit(s.charAt(i))) {
                num = num * 10 + s.charAt(i) - '0';
            }
            if (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ' || i == s.length() - 1) {
                if (sign == '+') {
                    stack.push(num);
                } else if (sign == '-') {
                    stack.push(-num);
                } else if (sign == '*') {
                    stack.push(stack.pop() * num);
                } else {
                    stack.push(stack.pop() / num);
                }
                num = 0;
                sign = s.charAt(i);
            }
        }
        return stack.stream().mapToInt(Integer::valueOf).sum();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {
            System.out.println(new Solution().calculate(in.nextLine()));
        }
    }
}

加强版

import java.util.Scanner;
import java.util.Stack;

class Solution {

    private static void push(Stack<Integer> stack, int num, char sign) {
        if (sign == '+') {
            stack.push(num);
        } else if (sign == '-') {
            stack.push(-num);
        } else if (sign == '*') {
            stack.push(stack.pop() * num);
        } else {
            stack.push(stack.pop() / num);
        }
    }

    private static int[] calc(char[] str, int index) {
        Stack<Integer> stack = new Stack<>();
        char sign = '+';

        int num = 0;
        while (index < str.length && str[index] != ')') {
            if (Character.isDigit(str[index])) {
                num = num * 10 + str[index++] - '0';
            } else if (str[index] == '(') {
                int[] next = calc(str, index + 1);
                num = next[0];
                index = next[1] + 1;
            } else if (str[index] != ' ') {
                push(stack, num, sign);
                num = 0;
                sign = str[index++];
            } else {
                index++;
            }
        }
        push(stack, num, sign);
        int sum = stack.stream().mapToInt(Integer::intValue).sum();
        return new int[]{sum, index};
    }

    public int calculate(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        return calc(s.toCharArray(), 0)[0];
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {
            System.out.println(new Solution().calculate(in.nextLine()));
        }
    }
}
posted @ 2021-12-18 23:44  Tianyiya  阅读(38)  评论(0)    收藏  举报