您的位置:首页 > 编程语言 > Java开发

[leetcode]Basic Calculator(java)

2015-07-07 18:13 656 查看
问题描述:

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.

通用算法是使用栈结构+后缀表达式(如代码1),但是看到csdn的一篇博客突然觉得这样好傻。因为这里面只有+和-,那么完全可以将()删除(代码2)。

这里面的思路是:使用一个栈,存放每个当前元素的系数(+1、-1),因为转变符号只有是-才会发生。

代码1:

[code] public int calculate(String s) {
         Stack<Character> charStack = new Stack<Character>();
         Stack<Integer> intStack = new Stack<Integer>();

         int index = -1;
         int length = s.length();
         char tmpChar;
         int top1,top2;
         while((++index)<length){
             if((tmpChar=s.charAt(index)) == ' ')//ignore blank
                 continue;
             switch (tmpChar) {
                case '(':
                    charStack.push(tmpChar);
                    break;
                case '-':
                case '+':
                    if(charStack.isEmpty())
                        charStack.push(tmpChar);
                    else if(charStack.peek()=='(')
                        charStack.push(tmpChar);
                    else {
                        char op = charStack.pop();
                        charStack.push(tmpChar);
                        switch (op) {
                            case '+':
                                top1 = intStack.pop();
                                top2 = intStack.pop();
                                intStack.push(top1+top2);
                                break;
                            default:
                                top1 = intStack.pop();
                                top2 = intStack.pop();
                                intStack.push(top2-top1);
                                break;
                        }
                    }
                    break;
                case ')':
                    while((tmpChar=charStack.pop())!='('){
                        switch (tmpChar) {
                        case '+':
                            top1 = intStack.pop();
                            top2 = intStack.pop();
                            intStack.push(top1+top2);
                            break;
                        case '-':
                            top1 = intStack.pop();
                            top2 = intStack.pop();
                            intStack.push(top2-top1);
                            break;
                        }
                    }
                    break;
                default://number
                    //form intnumber,and push to intstack
                    int number = 0;
                    while(tmpChar>='0'&&tmpChar<='9'){
                        number = number*10+tmpChar-'0';
                        if(++index<length)
                            tmpChar = s.charAt(index);
                        else
                            break;
                    }
                    intStack.push(number);
                    index--;
                    break;
                }
         }
         //process the last one
         while(!charStack.isEmpty()){
             tmpChar = charStack.pop();
             switch (tmpChar) {
                case '+':
                    top1 = intStack.pop();
                    top2 = intStack.pop();
                    intStack.push(top2+top1);
                    break;//+
                default:
                    top1 = intStack.pop();
                    top2 = intStack.pop();
                    intStack.push(top2-top1);
                    break;//-
            }
         }
         return (intStack.isEmpty())?0:intStack.pop();
      }


代码2:

[code] public int calculate(String s){
         int length = s.length();
         Stack<Integer> signStack  = new Stack<Integer>();
         signStack.push(1);//object
         signStack.push(1);//firstElement sign

         int index = -1;
         char tmpChar;
         int result = 0;

         while(++index<length){
             if((tmpChar=s.charAt(index))==' ')
                continue;//ignore blank
             switch (tmpChar) {
            case '+':
            case '(':
                signStack.push(signStack.peek());
                break;
            case '-':
                signStack.push(-1*(signStack.peek()));
                break;
            case ')':
                signStack.pop();
                break;
            default://digital
                int num=0;
                while(tmpChar>='0'&&tmpChar<='9'){
                    num=num*10+tmpChar-'0';
                    if(++index<length)
                        tmpChar = s.charAt(index);
                    else
                        break;
                }
                result+=signStack.pop()*num;
                index--;
                break;
            }
         }
         return result;
     }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: