Leetcode 224: Basic Calculator(基本计算器)

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

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

假定所给的表达式语句总是正确有效的。

例如:

“1 + 1” = 2
” 2-1 + 2 ” = 3
“(1+(4+5+2)-3)+(6+8)” = 23

思路:
每次遇到左括号、数字、加减号都入栈,遇到右括号出栈,直到弹出对应左括号,对于每次出栈后的子串,进行计算,再将结果重新入栈

class Solution {
    public int calculate(String s) {
        LinkedList<String> lk = new LinkedList<String>();
        for(int i=0;i<s.length();){
            if(s.charAt(i) == '('){
                lk.push("(");
                i++;
            }
            else if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){
                String tempNum = "";
                while(i<s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9'){
                    tempNum += s.charAt(i);
                    i++;
                }
                lk.push(tempNum);
            }
            else if(s.charAt(i) == ' '){
                i++;
            }
            else if(s.charAt(i) == '+' || s.charAt(i) == '-'){
                lk.push(s.charAt(i) + "");
                i++;
            }
            else{//右括号
                String str = lk.pop();
                String tempStr = "";
                while(!str.equals("(")){//直到左括号
                    tempStr = str + tempStr;
                    str = lk.pop();
                }
                //如果取出来可以运算则运算
                String leftSign = "";
                String rightSign = "";
                if(!"".equals(tempStr) && (tempStr.startsWith("-") || tempStr.startsWith("+"))){
                    leftSign = tempStr.substring(0,1);
                    tempStr = tempStr.substring(1);
                }
                if(!"".equals(tempStr) && (tempStr.endsWith("-") || tempStr.endsWith("+"))){
                    rightSign = tempStr.charAt(tempStr.length()-1) + "";
                    tempStr = tempStr.substring(1,tempStr.length()-1);
                }
                tempStr = cal(tempStr) + "";
                if(!"".equals(leftSign)){
                    lk.push(leftSign);
                }
                if(!"".equals(tempStr)){
                    lk.push(tempStr);
                }
                if(!"".equals(rightSign)){
                    lk.push(rightSign);
                }
                i++;
            }
        }
        String temp = "";
        while(!lk.isEmpty()){
            temp = lk.pop() + temp;
        }
        return cal(temp);
    }

    public int cal(String str){
        int preNum = 0;
        int i=0;
        int sign = 1;
        if(str.length() != 0 && (str.charAt(0) == '-' || str.charAt(0) == '+')){//第一个数是负数
            if(str.charAt(0) == '-'){
                sign = -1;
            }
            i++;
        }
        while(i< str.length() && str.charAt(i) != '+' && str.charAt(i) != '-'){
            preNum *= 10;
            preNum += str.charAt(i) - '0';
            i++;
        }
        preNum *= sign;
        while(i< str.length()){
            if(str.charAt(i) == '+' || str.charAt(i) == '-'){
                if(str.charAt(i) == '-'){
                    sign *= -1;
                }
                i++;
            }
            else{
                int tempNum = 0;
                while(i< str.length() && str.charAt(i) != '+' && str.charAt(i) != '-'){
                    tempNum *= 10;
                    tempNum += str.charAt(i) - '0';
                    i++;
                }
                if(sign == 1){
                    preNum = preNum + tempNum;
                }
                else{
                    preNum = preNum - tempNum;
                }
                sign = 1;
            }
        }

        return preNum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值