[leetcode]Basic Calculator(java)

原创 2015年07月07日 18:13:36

问题描述:
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:

 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:

 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;
     }
版权声明: 举报

相关文章推荐

[leetcode]Basic Calculator(java)

问题描述: Implement a basic calculator to evaluate a simple expression string.The expression string may...
  • zdavb
  • zdavb
  • 2015-07-07 18:13
  • 585

Leetcode之Basic Calculator & Basic Calculator II

Leetcode之basic calculator、basic calculator II

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

【Leetcode】Basic Calculator II

题目链接:https://leetcode.com/problems/basic-calculator-ii/题目: Implement a basic calculator to evaluat...

[LeetCode]Basic Calculator II

解题思路: 1,就是要写个简单的计算器呗 2,先把所有的 space去掉, 实现nonSpace方法; 3,把所有的 operator 和 operands分开,实现简单的 splitToOpe...

【LeetCode】Basic Calculator II

【LeetCode】Basic Calculator II Implement a basic calculator to evaluate a simple expression string. ...

LeetCode-224.Basic Calculator

https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...

leetcode 224 Basic Calculator

1. 问题描述  计算字符串表达式的值,表达式中只含有(,),+,-,空格和非负整数。例如:   “1 + 1” = 2   ” 2-1 + 2 ” = 3   “(1+(4+5+2)-3)+(...

Basic Calculator II -- leetcode

Implement a basic calculator to evaluate a simple expression string. The expression string contains...

[LeetCode] Basic Calculator

Basic Calculator Implement a basic calculator to evaluate a simple expression string. The ex...

Leetcode-224.Basic Calculator

Problem Description: Implement a basic calculator to evaluate a simple expression string. The expr...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)