美文网首页
2021-09-02leetcode

2021-09-02leetcode

作者: Cipolee | 来源:发表于2021-09-03 00:08 被阅读0次

代码写的乱,更容易被卡

class Solution:
    def calculate(self, s: str) -> int:
        #*和/拿出来算了再压进去
        #+和-压进去栈
        op_stack=[]
       
        #还是使用正则表达式简单点
        #正则表达式有点难
        t=''
        for i in s:
            if i==' ':
                continue
            t+=i
        s=t
        def com():
            t=0
            op_stack1=op_stack[::-1]
            while op_stack1:

                t=op_stack1.pop()
                try:
                    b=op_stack1.pop()
                    c=op_stack1.pop()
                    op_stack1.append(t+c if b=='+' else t-c)
                except:
                    pass
                
            return t
        i,n=0,len(s)
        #print(s)
        while i<n:
            print(op_stack)
            if s[i].isdigit():
                if i==0:
                    print(type(1)=='int')
                if len(op_stack) and type(op_stack[-1])==type(1):
                    #print('true')
                    
                    t=op_stack.pop()
                    op_stack.append(t*10+int(s[i]))
                else:
                    op_stack.append(int(s[i]))
            elif s[i]=='+' or s[i]=='-':
                op_stack.append(s[i])
            else:
                me=s[i]
                t=0
                i+=1
                while  s[i].isdigit():
                    
                    t+=int(s[i])
                    t*=10
                    if i<n:
                        i+=1
                    print(i,n)
                    if i==n:
                        t//=10
                        print(t)
                        t1=op_stack.pop()
                        op_stack.append(t*t1 if me=='*' else t1//t)
                        return com()
                t1=op_stack.pop()
                #print(t//10)
                t//=10
                op_stack.append(t*t1 if me=='*' else t1//t)
                i-=1
            i+=1
        print(op_stack)
        return com()

  • 思路:模拟栈,先把乘除计算完(合成数字),再算加减(需要做个逆序)


    超出时间限制

    因为使用sum(op_stack),所以要加入-num_per,故导致负数整除某整数的情况。并且按照题意是int(负数/整数)的形式。

  • 关于python中的堆,其只有小顶堆,大顶堆借助小顶堆实现


关于堆的打卡,直接一遍过,说明代码严谨性是可以的

  • 亮点,heapq小顶堆改造成大顶堆

设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。
示例:
输入: arr = [1,3,5,7,2,4,6,8], k = 4
输出: [1,2,3,4]
提示:
0 <= len(arr) <= 100000
0 <= k <= min(100000, len(arr))

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/smallest-k-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution:
    def smallestK(self, arr: List[int], k: int) -> List[int]:
        #最小的k个数
        #最小堆,应该使用最大堆来存
        if k==0 or not arr:
            return []
        heap_ch_min_max=[-i for i in arr[:k]]
        #print(heap_ch_min_max,arr[:k])
        #print(arr,k)
        heapq.heapify(heap_ch_min_max)
        
        for i in arr[k:]:
            try:
                t=heapq.heappop(heap_ch_min_max)
            except:
                #print(heap_ch_min_max)
                print('hi')
            if -i>t:
                heapq.heappush(heap_ch_min_max,-i)
            else:
                heapq.heappush(heap_ch_min_max,t)
        return [-i for i in heap_ch_min_max]


相关文章

  • 2021-09-02leetcode

    代码写的乱,更容易被卡 思路:模拟栈,先把乘除计算完(合成数字),再算加减(需要做个逆序)超出时间限制因为使用su...

网友评论

      本文标题:2021-09-02leetcode

      本文链接:https://www.haomeiwen.com/subject/uujcwltx.html