• LeetCode 每日一题 2023/10/23-2023/10/29


    记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步




    10/23 2678. 老人的数目

    取第12-13位数值

    def countSeniors(details):
        """
        :type details: List[str]
        :rtype: int
        """
        ans = 0
        for d in details:
            age = int(d[11:13])
            if age>60:
                ans +=1
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    10/24 1155. 掷骰子等于目标和的方法数

    递归 mem记录已有过的情况

    def numRollsToTarget(n, k, target):
        """
        :type n: int
        :type k: int
        :type target: int
        :rtype: int
        """
        if n>target or target>n*k:
            return 0
        MOD = 10**9+7
        mem = {(1,x):1 for x in range(1,k+1)}
        def check(n,target):
            if target<=0:
                return 0
            if (n,target) in mem:
                return mem[(n,target)]
            ans = 0
            if n==0:
                ans = target==0
            else:
                for i in range(1,k+1):
                    ans += check(n-1,target-i)
            mem[(n,target)] = ans
            return ans%MOD
        return check(n,target)
                
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    10/25 2698. 求一个整数的惩罚数

    遍历每个数 判断是否满足条件

    def punishmentNumber(n):
        """
        :type n: int
        :rtype: int
        """
        def check(s,i,x):
            m = len(s)
            if i>=m:
                return x==0
            y = 0
            for j in range(i,m):
                y = y*10+int(s[j])
                if y>x:
                    break
                if check(s,j+1,x-y):
                    return True
            return False
        ans = 0
        for i in range(1,n+1):
            if check(str(i*i),0,i):
                ans+=i*i
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    10/26 2520. 统计能整除数字的位数

    按每一位数字判断

    def countDigits(num):
        """
        :type num: int
        :rtype: int
        """
        ans = 0
        n = num
        while n:
            v = n%10
            if v>0 and num%v==0:
                ans +=1
            n//=10
        return ans
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    10/27 1465. 切割后面积最大的蛋糕

    将切口位置从小到大排序 计算两个切口间的距离
    寻找最大距离 相乘

    def maxArea(h, w, horizontalCuts, verticalCuts):
        """
        :type h: int
        :type w: int
        :type horizontalCuts: List[int]
        :type verticalCuts: List[int]
        :rtype: int
        """
        horizontalCuts.sort()
        verticalCuts.sort()
        if len(horizontalCuts)>0:
            maxh = max(horizontalCuts[0],h-horizontalCuts[-1])
        else:
            maxh = h
        if len(verticalCuts)>0:
            maxv = max(verticalCuts[0],w-verticalCuts[-1])
        else:
            maxv = w
        for i in range(1,len(horizontalCuts)):
            maxh = max(maxh,horizontalCuts[i]-horizontalCuts[i-1])
        for i in range(1,len(verticalCuts)):
            maxv = max(maxv,verticalCuts[i]-verticalCuts[i-1])
        return (maxv*maxh)%(10**9+7)
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    10/28 2558. 从数量最多的堆取走礼物

    大顶堆 每次取最大的礼物 进行处理

    def pickGifts(gifts, k):
        """
        :type gifts: List[int]
        :type k: int
        :rtype: int
        """
        import heapq,math
        ans = sum(gifts)
        l = [-x for x in gifts]
        heapq.heapify(l)
        for i in range(k):
            print(l)
            v = -heapq.heappop(l)
            num = int(math.sqrt(v))
            ans -= (v-num)
            heapq.heappush(l, -num)
            
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    10/29 274. H 指数

    将引用次数从小到大排序 从小开始判断是否满足

    def hIndex(citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        citations.sort()
        n = len(citations)
        for i in range(len(citations)):
            if citations[i]>=n-i:
                return n-i
        return 0
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

  • 相关阅读:
    [html]当网站搭建、维护的时候,你会放个什么界面?
    计算机网络 | 05.[HTTP] SSL/TLS握手过程
    WebSocket: 实时通信的理解与认识
    力扣:第 304 场周赛
    【计算机网络】https协议
    AcWing基础部分Class2:高精度加减乘除、前缀和与差分
    在微信小程序中怎么做投票活动
    web相关框架
    2023年中国乳胶制品产量、需求量及市场规模分析[图]
    网络工程师回顾学习(第三部分)
  • 原文地址:https://blog.csdn.net/zkt286468541/article/details/134050595