Leetcode 227 Basic Calculator II

本文介绍了一个简单的表达式计算器实现,该计算器能够解析并计算包含基本算术运算符(加、减、乘、除)的非负整数表达式。文章通过示例展示了如何使用栈来处理操作数和运算符,并提供了完整的Java代码实现。

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +-*/ operators and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5

Note: Do not use the eval built-in library function.


public class Solution227 {
    public int calc(int a, int b, char ch) {
        if(ch == '+') return a+b;
        else if(ch == '-') return a-b;
        else if(ch == '*') return a*b;
        else if (ch == '/') return a/b;
        return 0;
    }
    public int calculate(String s) {
        Map<Character,Integer> map = new HashMap<Character, Integer>();
        map.put('+', 1);
        map.put('-',1);
        map.put('*', 2);
        map.put('/',2);

        Stack<Integer> number = new Stack<Integer>();
        Stack<Character> operator = new Stack<Character>();

        if (s.length() <= 0 || s == null) return 0;

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);

            if(ch == '+' || ch == '-' || ch == '*' || ch == '/') {
                while (!operator.isEmpty()) {
                    Character top = operator.peek();
                    if (map.get(ch) <= map.get(top)) {
                        top = operator.pop();
                        int num2 = number.pop(), num1 = number.pop();
                        int ans = calc(num1, num2, top);
                        number.push(ans);
                    } else {
                        break;
                    }
                }
                operator.push(ch);

            }else if(ch >= '0' && ch <= '9') {
                int tmp = 0;

                while (i < s.length()) {
                    ch = s.charAt(i);
                    if(ch >= '0' && ch <= '9') {
                        tmp = tmp * 10 + ch - '0';
                    } else {
                        break;
                    }
                    i += 1;
                }
                number.push(tmp);
                //System.out.println("**"+tmp);
                i -= 1;
            }
        }

        int ans = number.peek();

        while (!operator.isEmpty()) {
            char ch = operator.pop();
            int num2 = number.pop(), num1 = number.pop();
             ans = calc(num1, num2, ch);
            number.push(ans);
        }

        return ans;
    }
    public static void main(String[] args) {
        Solution227 ans = new Solution227();
        String str = " 1*2-3/4 + 5*6 - 7*8 + 9/10";
        int res = ans.calculate(str);
        System.out.println(res);
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值