• 数据结构与算法(1)支线任务2——Basic Calculator


    题目:
    https://leetcode.com/problems/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.

    思路一:

    按照运算符优先级将原表达式转化为波兰表达式后缀式,然后再计算后缀式,与之前作业方法一致。

    然而“前人”的博客已经写得很好了(见文末链接),上节课又刚好讲了递归,于是有了另一个作死的思路。

    思路二:

    计算只有加减的表达式,遇到括号递归调用该函数。关键部分如下:

        for (i = 0; i < ori.size() - c; i++)
        {
            a = ori[i+c];
            switch (a)
            {
            //为简洁删掉其余case和default部分的代码
            case '(':
                str.assign(ori, i+c+1, ori.size()-i-c-1);
                c += getOp(str, m)+1;
                elem.e.m = m;
                elem.isChar = 0;
                op.push_back(elem);
                break;
            case ')':
                while (!s.empty())
                {
                    t = s.top();
                    s.pop();
                    elem.e.c = t;
                    elem.isChar = 1;
                    op.push_back(elem);
                }
                n = calOp(op);
                return i;
                break;
            }
        }

    然而在括号过多时,算法复杂度为O(n2),遇到文章最后的给出的输入会超时(此处不想吐槽……)

    思路三:

    对该题目,其实只需要判断每个数应该进行的操作('+'/'-'),于是可以只用一个用于记录当前(括号外)的加或减的栈,再对括号内每个数判断其前面的符号进行相应操作。这样只需遍历一次,复杂度O(n)。不过还要注意空格的问题。代码如下:

    int calculate(string s) {
        stack<bool> isPos;//当前括号外应执行的操作,'+'为1
        char c;
        bool curPos = 1;//当前待执行的操作,'+'为1
        isPos.push(1);//初始操作为'+'
        int sum = 0, t;
        for (int i = 0; i < s.size(); i++)
        {
            c = s[i];
            switch (c)
            {
            //判断当前应执行的操作
            case '+':
                if (isPos.top() == 0)
                {
                    curPos = 0;
                } 
                else
                {
                    curPos = 1;
                }
                break;
            case '-':
                if (isPos.top() == 0)
                {
                    curPos = 1;
                } 
                else
                {
                    curPos = 0;
                }
                break;
            //遇到左括号更新括号外的操作
            case '(':
                isPos.push(curPos);
                break;
            //遇到右括号弹出当前括号外的操作,恢复为上一个括号内
            case ')':
                isPos.pop();
                break;
            //遇到数字继续扫描至符号
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                t = 0;
                while (s[i] >= '0' && s[i] <= '9')
                {
                    t = 10*t + s[i] - '0';
                    i++;
                }
                i--;
                if (curPos == 0)
                {
                    sum -= t;
                } 
                else
                {
                    sum += t;
                }
                break;
            //忽略其他的符号(其实只有空格)
            default:
                break;
            }
        }
        return sum;
    }

    ”前人“们的博客:

    http://200404.sinaapp.com/2015/10/evaluating-an-arithmetic-expression/
    http://www.cnblogs.com/lustralisk/p/brance-2.htm
    http://jmq14.github.io/2015/10/23/calculator/

    思路二没通过的例子:

    "(9-(10-(10-0-(3+(8+(0+(8-(10-8-(7-(2+(5+(6+(10+(3+(8+(3-(9+(1+(10+(1-(1+(6-2+0+(10-(9-(3-(3-9-(1-(7+(4-(2+(2-(10+(3+(7-(1-(4+(1+(1-(10-(5-(9+(9-4-(5-(1+8-(2-(1+(1-10-(4-(1+(4-(7)-(3-(8)+(5+5-(5-(9-(8+(8-4-1+(0-(1+(1+(10-(7+(2-(5-(4-(6+(2+(1-(2-(9+8+(2+(9-(9-(7+(10+1+(5)))-(2-(8+3+(5-(7-(3+(9)+(10+(0+(8-(1-(9)-(0+10-(3+(9-(0-(5-(7-(4-4+1+(7)-(10+(5+(9-(3+(5+(6-(0-(7-(1-(4+(6+(4-2-(4+(9-(6+9-8+1+(5+(7-9+3)+(10-(10+(2+(0-(5-(2+(10-(4-5-(7-(4-(7+(4)+6+10+(2-(7+(2))+(1)+(5-(7)-(10-(5+(7-(6-(2+(1-4)+(10-(5)+(4+(10+(4+(0+(10+(8-(8+(6+5-(1-(6-(1-(2+(4+(9-(3+(1+(10+(4)+(0+(3-(2-(9-(2-(3-(4-(2+(7-(6-(5+(7+(5+(5-(4+(0-(7+(2-(7+(9)-(6-(10)+(7+(2-(9-(9)+(4+(1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(6+(5)))))-(8-(6)-(10+(5-(8)-(10+(3+(0+(6-(9)-(1)))-(0)-(9+(0+(1+(8+2-(4-(9-(4+(3+4)-(10+(1-(5)+(10-(4-(6-(4-(2+(4)-(9)-(4))))-3))))+(9)+(9+(0-(1+(5-(5+(7)-6-(8-(3-(3+(1)-(9-(7-(6)))-(2+(1))-(1+(2+(10))))+(6)+(0+(9-(1)-(10)))+(10-(1-(1)))-(0+(0-(2-(4-(6+(1))+(0)+(5)-(5+(5)-(4+(6)-(5)+(1-(7))))+(8)-(7))-3))-(7+(7+(9+(0+(10)-(7-(0-(2)-(6))-(2+(10)))+(7)+(3))+(8-(8+(10)-(8)+(0+(6-(2)-(1))+(3+(10+(10-(4+(7-(2)-(9-(2+(8))))+(7)-(7+10+(9-(2)+(0))-(6+(1)))+(10)+(2)-(7)-(4)-(10+(3-(6))))+(8-(1))-(10)))+(5+(3-(0-(1-(2+(3-(6-(4)-(1)+(4+(7+(3)-(7)+(4-(9))+(0-(4)+(9+(3-(9)+(4-(10+(6+(4)))+(4))+(10+(0-3-(8+(0-(6))-(5))-(9))-(6))))+2))+6+(6)+(1-(6))-(7-(1))-(8)+(9-(8))+(4)))-(0+7-(1)))-(2))+(0)))+(4-(7))-(5)-(8)-4+(1-(3-(8+(2+0)+(7)))))))-(4-(2))+(9))))+(7)-(2-(10+(4)-(8+(7)+(5-(4)-(6+6))-(2+(6)-(2+(4-(2-(8-(4)-(7+(5)-(10-(7)))))-(10+(9+(8)-(10)+(3-(7+(4+(2+(5)-(10+7+(2-(10)-(10+(3))+(0-(10+(8+(4+(7-(2)+(3+9))))+(7-(6+(2)-(2)+7+(5+(7+(10+(5-(4)-2+(5)+(1))+(0))))-(9))-5-(8)-(9-(4)-(10))-(8-(5)-(10)-7)+(5))-(4)))))+6+3+(3+(6+(9)))-10+(6)+(0)))))+(7)))+(1-(5)+(3-(3+6))+(5)+(7)-(9-(1))+(4+(1))+(2)))-(3))-(10)+(1)))))))))+(3)+2+(8-(4)))-(1))+(6-(8-(0)-(8-(0))-(2-(4+2)))-(9+1)))-(8-(8+(1-8-(7))))+7-(5+(5+(6+(10)+(8)))))))-(4))))-(4)-(6)+(10)-(5)))+(0+(2+(4))-(4-(2)+(0-(10-(4))))))+3-(10)))-(9+(9-(8-(7)-4))))+(6))-(4-(9))))-(1))+(10))))-(0+(9+7-(1)))))-(7)-(4)))-(9))))-7))))+(9))+(10))-(8-(9)))+(8))-(6)-(4)-(8)))))))))))))-(7)))))+(2-(6)-(0))))-(0)-(5+(9)+(9))+(3-(9))))+(8))))))))-(0-(0))+(7-(2))))))))-(6))-(8+(9))-(9+(2))-(2)+(9))-(4))+(7)-(1)-(6))-(2-0)))))))-(0)))))-(8+(0-(5))))+(9)-(1-(0)-(3)))-(3)-(0)))+(4)+(6))))-(5)+(1-(5)))))+(10))))-(5)+(0))))))-(6)))))))+(1))))))))-(5)))))))))+(8))))))))))))))))))-(7)+(10)))))))))))))-(4))))))-(10)-(4))+(1)+(3))-(1))))+(9))))))))+(2-(7-(4-(3+(0))))-(10)))))+0))))+(10)))))+(4)))))))))))+(3)))))))-(5)))))+(3)))))))))))))-(7)-(5-(2+(9))-(0))+(4)))+(10)))))-(1)))-(0))+(1))-(8+(10))))))-(10)-(10+(9)+(2))))-(1)))-(2))))+(4+(5))))))+(8))))))))))))))))))))))-(7)))-(3)))))))+(1))))-(7)-(3)+(4))))))-(6)))))))-(9-(3)))))))))))+(8))))))))))+(6))))))))))))))))))))))))))))))))))+(5))+(7))))))))))))))))))))))))-(10))))))+9)))))))"

  • 相关阅读:
    【css】所有的a标签设置为新窗口打开【原创】
    TP 3.1版本不支持 CONTROLLER_NAME
    关于TP 特殊页面伪静态规则的编写 研究实现
    larave PHP框架
    [转]git和github
    [转]mysql语句大全
    camera.swf?  提示  图片上传中请稍候,没任何报错
    mysql大小写敏感(默认为1,不敏感)
    数据cube的schema与sql的对应的关系
    MDX的实例讲解(排名前15的小例子)
  • 原文地址:https://www.cnblogs.com/permitato/p/4918941.html
Copyright © 2020-2023  润新知