[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-224. Basic Calculator (JAVA)实现计算器

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

LeetCode-227. Basic Calculator II (JAVA)实现计算器 II

LeetCode-227. Basic Calculator II (JAVA)实现计算器 II

LeetCode Basic Calculator(用栈计算表达式的值)

题意:给出一个计算表达式,只包含 +,-,(,),求计算结果 思路:用栈来实现  代码如下: public class Solution { private int cal(int nu...

第一周 leetcode 224. Basic Calculator(hard)

第一周 leetcode 224. Basic Calculator(hard)题目描述: Implement a basic calculator to evaluate a simple ex...
  • renzhff
  • renzhff
  • 2017年03月13日 20:25
  • 87

Leetcode Basic Calculator 系列

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

leetcode_Basic Calculator

题目: Implement a basic calculator to evaluate a simple expression string. The expression stri...
  • sunp823
  • sunp823
  • 2015年06月30日 23:43
  • 185

【leetcode】String——Basic Calculator II (227)

题目: Implement a basic calculator to evaluate a simple expression string. The expression string...

LeetCode No.224 Basic Calculator

LeetCode No.224 Basic Calculator

LeetCode(224) Basic Calculator

题目Implement a basic calculator to evaluate a simple expression string.The expression string may cont...
  • fly_yr
  • fly_yr
  • 2015年12月09日 14:00
  • 768

Leetcode: Basic Calculator

 Implement a basic calculator to evaluate a simple expression string. The expression string may ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode]Basic Calculator(java)
举报原因:
原因补充:

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