zoukankan      html  css  js  c++  java
  • *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


    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:

    1. digit: it should be one digit from the current number
    2. '+': number is over, we can add the previous number and start a new number
    3. '-': same as above
    4. '(': push the previous result and the sign into the stack, set result to 0, just calculate the new result within the parenthesis.
    5. ')': 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.

    public class Solution {
        public int calculate(String s) 
        {
            int number = 0;
            int result = 0;
            int sign = 1;
            Stack<Integer> stack  = new Stack<Integer>();
            for(int i=0;i<s.length();i++)
            {
                char c = s.charAt(i);
                if(Character.isDigit(c))
                {
                    number = number * 10 + (c-'0');
                }
                else if (c=='+')
                {
                    result += sign*number;
                    number = 0;
                    sign = 1;
                }
                else if (c=='-')
                {
                    result += sign*number;
                    number = 0;
                    sign = -1;
                }
                else if (c=='(')
                {
                    stack.push(result);
                    stack.push(sign);
                    result = 0;
                    sign = 1;
                }
                else if (c==')')
                {
                    result += sign*number;
                    number = 0;
                    result *= stack.pop();
                    result += stack.pop();
                }
            }
            
            if(number != 0) result += sign * number;
            return result;
        }
    }

    reference: https://leetcode.com/discuss/39553/iterative-java-solution-with-stack

  • 相关阅读:
    std 迭代器的几种用法
    Cocos2dx3.1+xcode +lua配置
    php获取文件创建时间、修改时间
    如何创建一个基于jquery的编辑器
    封装ajax,简单的模仿jquery提交
    如何实现SQL事务的提交,又不对外进行污染
    .net mvc结合微软提供的FormsAuthenticationTicket登陆
    Http GET、Post方式的请求总结
    post 报文请求接口方法
    zip格式压缩、解压缩(C#)
  • 原文地址:https://www.cnblogs.com/hygeia/p/5135534.html
Copyright © 2011-2022 走看看