Basic Calculator

这道题目真是花了九牛二虎之力才做对。。。= = 先转成逆波兰式再计算。。。这里的版本是完整的计算器功能,包括所有运算符, I && II都适用。不多说了,心很累,直接上代码:

public class Solution {
    public static int calculate(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }

        String str = s.replaceAll("\\s","");
        List<String> postfix = convertToPostfix(str);
        Stack<Integer> stack = new Stack<Integer>();
        for (int i = 0; i < postfix.size(); i++) {
            String current = postfix.get(i);
            if (current.equals("+") || current.equals("-") || current.equals("*")
                    || current.equals("/")) {
                Integer num1 = stack.pop();
                Integer num2 = stack.pop();
                if (current.equals("-")) {
                    stack.push(num2-num1);
                } else if (current.equals("+")) {
                    stack.push(num2 + num1);
                } else if (current.equals("*")) {
                    stack.push(num2 * num1);
                } else {
                    stack.push(num2 / num1);
                }
            } else {
                stack.push(Integer.parseInt(current));
            }
        }
        return stack.pop();
    }

    private static List<String> convertToPostfix(String str) {
        Stack<Character> stack = new Stack<Character>();
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < str.length(); i++) {
            char current = str.charAt(i) ;
            StringBuilder sb = new StringBuilder();
            if (Character.isDigit(current)) {
                while (i < str.length() && Character.isDigit(str.charAt(i))) {
                    sb.append(str.charAt(i));
                    i++;
                }
                i--;
                list.add(sb.toString());
            } else if (current == '(') {
                stack.push(current);
            } else if (current == ')') {
                while (!stack.isEmpty() && stack.peek() != '(') {
                   list.add(Character.toString(stack.pop()));
                }
                stack.pop();
            } else {
                if (stack.isEmpty()) {
                    stack.push(current);
                } else {
                    while (!stack.isEmpty()) {
                        char temp = stack.peek();
                        if (temp == '(' || (current == '*' && (temp == '+' || temp == '-'))
                                || (current == '/' && (temp == '+' || temp == '-'))) {
                            break;
                        } else {
                            list.add(Character.toString(stack.pop()));
                        }
                    }
                    stack.push(current);
                }
            }
        }

        while (!stack.isEmpty()) {
            list.add(Character.toString(stack.pop()));
        }
        return list;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值