leetcode之Basic Calculator

原创 2015年11月18日 00:56:16

这道题一开始不太会,各种蛋疼。最近在看数据结构,看到了用栈来解的思路。用python来实现以下吐舌头。代码如下:

class Solution(object):
    def calculate(self, s):
        """
        :type s: str
        :rtype: int
        """
        import re
        listofnum = []
        listofsym = []
        symbol = ['(', ')', '+', '-']
        #空集
        if len(s) == 0:
            return 0
        #一个字母
        try:
            return int(s)
        except:
            #将所有项分别列出来
            s = re.sub(' ', '', s)
            n = ''
            for i in range(len(s)):
                try:
                    if s[i] in symbol:
                        listofnum.append(int(n))
                        listofsym.append(s[i])                        
                        n = ''
                    elif i == len(s) - 1:
                        n = n + s[i]
                        listofnum.append(int(n))
                    else:
                        n = n + s[i]
                except:
                    listofsym.append(s[i])
                #计算括号
                if s[i] == ')':
                    listofsym.pop()
                    while listofsym[-1] != '(':
                        a = listofnum.pop()
                        b = listofnum.pop()
                        if listofsym[-1] == '-':
                            if listofsym[-2] == '-':
                                listofnum.append(a + b)
                            else:
                                listofnum.append(b - a)
                        else:
                            if listofsym[-2] == '-':
                                listofnum.append(b - a)
                            else:
                                listofnum.append(b + a)
                        listofsym.pop()
                    listofsym.pop()
            #计算括号之外的
            while len(listofsym) != 0:
                a = listofnum.pop()
                b = listofnum.pop()
                try:
                    if listofsym[-1] == '-':
                        if listofsym[-2] == '-':
                            listofnum.append(a + b)
                        else:
                            listofnum.append(b - a)
                    else:
                        if listofsym[-2] == '-':
                            listofnum.append(b - a)
                        else:
                            listofnum.append(b + a)
                    listofsym.pop()
                except:
                    if listofsym[-1] == '-':
                        return b - a
                    else:
                        return a + b
            return listofnum[0]


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

第一周 leetcode 224. Basic Calculator(hard)

第一周 leetcode 224. Basic Calculator(hard)题目描述: Implement a basic calculator to evaluate a simple ex...

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...

【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...

Leetcode: Basic Calculator

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

[LeetCode]Basic Calculator II

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

Basic Calculator II -- leetcode

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

LeetCode 227. Basic Calculator II

LeetCode 227. Basic Calculator II 时间复杂度O(n),空间复杂度O(1)c++
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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