leetcode之Basic Calculator II

原创 2015年11月20日 00:02:06
跟Basic Calculator I差不多,只不过这次是增加了乘除,运用数据结构的基础知识做题。代码如下:
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] in ['+', '-']:
                    a = listofsym.pop()
                    if listofsym != []:
                        if listofsym[-1] in ['*', '/']:
                            listofnum1 = []
                            listofsym1 = []
                            listofnum1.append(listofnum.pop())
                            try:
                                while listofsym[-1] not in ['+', '-']:
                                    listofsym1.append(listofsym.pop())
                                    listofnum1.append(listofnum.pop())
                            except:
                                listofnum1 = listofnum1
                                listofsym1 = listofsym1
                            for i in listofsym1[::-1]:
                                a1 = listofnum1.pop()
                                b1 = listofnum1.pop()
                                if i == '*':
                                    listofnum1.append(a1 * b1)
                                else:
                                    if a1 > 0:
                                        if b1 > 0:
                                            listofnum1.append(a1 / b1)
                                        else:
                                            listofnum1.append(-(a1 / (-b1)))
                                    else:
                                        if b1 > 0:
                                            listofnum1.append(-(-a1 / b1))
                                        else:
                                            listofnum1.append(a1 / b1)
                            listofnum.append(listofnum1[0])
                    listofsym.append(a)
            #计算最后的
            if listofsym[-1] in ['*', '/']:
                listofnum1 = []
                listofsym1 = []
                listofnum1.append(listofnum.pop())
                while listofsym != []:
                    if listofsym[-1] not in ['+', '-']:
                        listofsym1.append(listofsym.pop())
                        listofnum1.append(listofnum.pop())
                    else:
                        break
                for i in listofsym1[::-1]:
                    a1 = listofnum1.pop()
                    b1 = listofnum1.pop()
                    if i == '*':
                        listofnum1.append(a1 * b1)
                    else:
                        if a1 > 0:
                            if b1 > 0:
                                listofnum1.append(a1 / b1)
                            else:
                                listofnum1.append(-(a1 / (-b1)))
                        else:
                            if b1 > 0:
                                listofnum1.append(-(-a1 / b1))
                            else:
                                listofnum1.append(a1 / b1)
                listofnum.append(listofnum1[0])
            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 No.227 Basic Calculator II

LeetCode No.227 Basic Calculator II

Leetcode 227 Basic Calculator II

简单计算器

LeetCode[227] Basic Calculator II

实现一个基本的计算器 https://leetcode.com/problems/basic-calculator-ii/支持简单的’+’,’-‘,’*’,’/’功能。之前在学数据结构的Stack时有...

LeetCode227. Basic Calculator II

题目链接: https://leetcode.com/problems/basic-calculator-ii/ 题目描述:计算字符串表达式的值。可能会有多余空格。题目分析:这道题对于我来说坑略多...
  • codeTZ
  • codeTZ
  • 2016年01月22日 17:50
  • 236

Leetcode 227 Basic Calculator II

1. 问题描述  给遗传字符串类型的表达式,计算表达式的值。表达式只包含+、-、*、/和空格。   2. 方法与思路  此题和Basic Calculator I类似,只是左右括号换成了乘除符号。基...

[Leetcode]Basic Calculator II

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

leetcode 227. Basic Calculator II

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

《leetCode》:Basic Calculator II

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

leetcode -- Basic Calculator I &II --重点,未完全理解

Basic Calculatorhttps://leetcode.com/problems/basic-calculator/此题目要求没有负号,所以降低了难度思路1 转换成reverse polis...
  • xyqzki
  • xyqzki
  • 2015年12月16日 12:33
  • 298

Leetcode-227.Basic Calculator II

Problem Description: Implement a basic calculator to evaluate a simple expression string. The expr...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode之Basic Calculator II
举报原因:
原因补充:

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