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;
        }
    }
  • 相关阅读:
    easyui 动态列
    easyui editor combobox multiple
    千年老二
    Win7上Git安装及配置过程
    Tomcat自动部署
    Win7下安装git
    修改mysql默认字符集的方法
    oracle 查询当前库中所有表以及某表字段信息
    treegrid and datagrid ctrl or shift selectRow
    条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4712998.html
Copyright © 2011-2022 走看看