zoukankan      html  css  js  c++  java
  • [leedcode 224] Basic Calculator

    Implement 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 .

    You may assume that the given expression is always valid.

    Some examples:

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

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

    public class Solution {
        public int calculate(String s) {
            /*首先...这是十以内单值计算还是10 以上,存在 两位数??! 存在两位数的话还是比较麻烦的....果然不是10以内,那么如何判断数字结束了呢?
            Simple iterative solution by identifying characters one by one. One important thing is that the input is valid, which means the             parentheses are always paired and in order.Only 5 possible input we need to pay attention:
            只是遍历一边string,然后判断每一个character.
            digit: it should be one digit from the current number
            '+': number is over, we can add the previous number and start a new number
            '-': same as above
            '(': push the previous result and the sign into the stack, set result to 0, just calculate the new result within the parenthesis.
            ')': pop out the top two numbers from stack, first one is the sign before this pair of parenthesis, second is the temporary result          before this pair of parenthesis. We add them together.
            Finally if there is only one number, from the above solution, we haven't add the number to the result, so we do a check see if the          number is zero
            
            构造一个栈,保存的是中间结果和符号,当遇到左括号时,保存左括号之前的结果和符号,res置空,计算空号内的结果,遇到右半括号,弹出栈顶两个元素,与括号内结果相加。本题需要注意的是,当遇到+或-号,计算的是上一次运算,此时只需要保存sigh和将sum置空即可*/
            Stack<Integer> stack=new Stack<Integer>();
            int res=0;
            int sigh=1;
            int num=0;
            for(int i=0;i<s.length();i++){
                char c=s.charAt(i);
                if(Character.isDigit(c)){
                    num=10*num+(int)(c-'0');
                }else if(c=='+'){
                    res+=num*sigh;
                    num=0;
                    sigh=1;
                    
                }else if(c=='-'){
                    res+=num*sigh;
                    num=0;
                    sigh=-1;
                }else if(c=='('){
                    stack.push(res);
                    stack.push(sigh);
                    res=0;
                    sigh=1;
                }else if(c==')'){
                    res+=num*sigh;
                    num=0;
                    res*=stack.pop();
                    res+=stack.pop();
                }
                
            }
            if(num!=0){
                res+=sigh*num;
            }
            return res;
        }
    }
  • 相关阅读:
    DotNet友元程序集解析
    fastadmin如何在列表操作列区域添加按钮及控制已有按钮显示
    PHP合成透明图片
    linux系统下执行定时任务的全过程
    关于阿里云简单文件上传OSS思路整理服务器上的文件上传到OSS
    PHP图片和文字合成函数刚刚出炉
    关于在fastadmin后台AJAX上传图片或者视频增加额外参数的办法
    标记一下关于fastadmin在列表页获取视频时长并且AJAX提交到后端正常显示的过程
    关于phpexcel导出65535的解决思路
    PHP原生代码集成腾讯云对象存储 COS整个过程源码方式
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4712998.html
Copyright © 2011-2022 走看看