关闭

LeetCode 224 Basic Calculator (后缀表达式 栈)

标签: leetcode后缀表达式
888人阅读 评论(0) 收藏 举报
分类:

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.

题目链接:https://leetcode.com/problems/basic-calculator/

题目分析:最中规中矩的做法就是,先中缀表达式转后缀表达式,然后根据后缀表达式计算答案,Java跑了100ms,算是很慢了,中缀转后缀的方法是:

1.若为数字直接加到后面

2.若为'(',入符号栈

3.若为运算符,则将优先级大于等于它的运算符先弹出并记录带答案,再将其入栈,本题运算符只有+,-,优先级相同

4.若为')',弹出运算符直到遇到‘(’

因为可能存在不止1位的数字,所以用一个空格来区分,还有就是连字符串的时候StringBuffer要比String快得多

public class Solution {
    
    public void solve(Stack<Integer> stkNum, char op) {
        int a = stkNum.peek();
        stkNum.pop();
        int b = stkNum.peek();
        stkNum.pop();
        if(op == '+') {
            stkNum.push(b + a);
        }
        else if(op == '-') {
            stkNum.push(b - a);
        }
    }
    
    public String infixToSuffix(String s) {
        StringBuffer sb = new StringBuffer("");
        Stack<Character> stkOp = new Stack<>();
        int num, len = s.length();
        for(int i = 0; i < len; i ++) {
            char ch = s.charAt(i);
            if(ch == ' ') {
                continue;
            }
            else if(ch == '(') {
                stkOp.push(ch);
            }
            else if(ch == '+' || ch == '-') {
                while(stkOp.size() > 0 && stkOp.peek() != '(') {
                    sb.append(stkOp.peek());
                    stkOp.pop();
                }
                stkOp.push(ch);
            }
            else if(ch == ')') {
                while(stkOp.peek() != '(') {
                    sb.append(stkOp.peek());
                    stkOp.pop();
                }
                stkOp.pop();
            }
            else {
                num = 0;
                while(i < len && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                    sb.append(s.charAt(i));
                    i ++;
                }
                sb.append(' ');
                i --;
            }
        }
        return sb.toString();
    }
    
    public int calculate(String s) {
        Stack<Integer> stkNum = new Stack<>();
        String str = infixToSuffix('(' + s + ')');
        int num = 0;
        //System.out.println("str = " + str);
        for(int i = 0; i < str.length(); i ++) {
            if(str.charAt(i) == '+') {
                solve(stkNum, '+');
            }
            else if(str.charAt(i) == '-') {
                solve(stkNum, '-');
            }
            else {
                num = 0;
                while(str.charAt(i) != ' ') {
                    num = num * 10 + str.charAt(i) - '0';
                    i ++;
                }
                stkNum.push(num);
            }
        }
        return stkNum.peek();
    }
}

本题也可以不用栈,直接用递归解决递归的去处理每对括号,参考discuss里大神做法,短小精悍只跑了3ms

public class Solution {
    public static int calculate(String s) {
        if (s.length() == 0) {
            return 0;
        }
        s = "(" + s + ")";
        int[] pos = {0};
        return eval(s, pos);
    }

    private static int eval(String s, int[] pos) {
        int val = 0, i = pos[0], sign = 1, num = 0;
        while(i < s.length()) {
            char c = s.charAt(i);
            switch(c) {
                case '+': val = val + sign * num; num = 0; sign = 1; i++; break;
                case '-': val = val + sign * num; num = 0; sign = -1; i++; break;
                case '(': pos[0] = i + 1; val = val + sign * eval(s, pos); i = pos[0]; break;
                case ')': pos[0] = i + 1; return val + sign * num; 
                case ' ': i++; continue;
                default : num = num * 10 + c - '0'; i++;
            }
        }
        return val;
    }
}


0
0
查看评论

堆栈实现中缀表达式转后缀表达式及计算表达式的值

算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。算术表达式我们的普通表达式,后缀表达式不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行,( 3 + 4 ) × 5 - 6 输入格式说明: 输入在一行内给出不超过30个字符的前缀表达式,包含...
  • jiangjieqazwsx
  • jiangjieqazwsx
  • 2015-01-26 11:19
  • 695

Leetcode 224: Basic Calculator

Leetcode 224: Basic Calculator java c++ python
  • xudli
  • xudli
  • 2015-06-19 00:59
  • 7588

LeetCode224:Basic Calculator

Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or ...
  • u012501459
  • u012501459
  • 2015-07-15 16:04
  • 675

【LeetCode】Basic Calculator && Basic Calculator II

1、Basic Calculator  Total Accepted: 3726 Total Submissions: 24053 My Submissions Question Solution  Implement a basic calculator to evaluate...
  • u013027996
  • u013027996
  • 2015-06-24 11:01
  • 3519

LeetCode[227] Basic Calculator II

实现一个基本的计算器 https://leetcode.com/problems/basic-calculator-ii/支持简单的’+’,’-‘,’*’,’/’功能。之前在学数据结构的Stack时有提到过如何用栈来实现计算器,就是用两个栈:操作数栈和运算符栈。写了一上午才AC,有两个问题。 第一...
  • hust_dxxxd
  • hust_dxxxd
  • 2016-05-04 11:29
  • 377

LeetCode 227. Basic Calculator II 解题报告【python】

思路分析 该题是一个简单的表达式求值问题,我们可以采用“算符优先法”来解决该问题。该算法在严蔚敏的《数据结构C语言版》第三章有详细描述。 该题中的每一个表达式都是由操作数和操作符组成,根据四则运算法则中的“先算乘除,后算加减”原则,任意两个相继出现的操作符op1和op2之间的优先关系至多是下面的三种...
  • kuaisuzhuceh
  • kuaisuzhuceh
  • 2016-01-30 14:11
  • 610

LeetCode(224) Basic Calculator

本题并没有什么难度,就是稍微烦点 c++代码如下class Solution { public: int calculate(string s) { //if(s == "(1)") return 1; //if(s == "1+...
  • guanzhongshan
  • guanzhongshan
  • 2015-08-16 17:16
  • 155

[LeetCode 224]Basic Calculator

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses...
  • sbitswc
  • sbitswc
  • 2015-08-27 10:47
  • 484

【LeetCode】Basic Calculator 解题报告

【题目】 Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing paren...
  • ljiabin
  • ljiabin
  • 2015-06-14 11:19
  • 8287

leetcode 227: Basic Calculator II

leetcode 227: Basic Calculator II PYTHON JAVA C++
  • xudli
  • xudli
  • 2015-06-26 03:18
  • 6902
    个人资料
    • 访问:746804次
    • 积分:15367
    • 等级:
    • 排名:第874名
    • 原创:818篇
    • 转载:7篇
    • 译文:0篇
    • 评论:123条
    博客专栏