【算法】力扣第 283 场周赛(最短代码)

6016. Excel 表中某个范围内的单元格

看数据范围,直接一行

class Solution:
    def cellsInRange(self, s: str) -> List[str]:
        return [chr(i)+str(j) for i in range(ord(s[0]),ord(s[3])+1) for j in range(int(s[1]),int(s[4])+1)]

6017. 向数组中追加 K 个整数

贪心,等差数列求和,三行解法👇

class Solution:
    def minimalKSum(self, nums: List[int], k: int) -> int:
        nums,res,last,pt = sorted(set(nums)),(1+k)*k//2,k,0
        while pt < len(nums) and nums[pt] <= last:last,res,pt = last+1,res+last-nums[pt]+1,pt+1
        return res

六弦爷两行题解👇

class Solution:
    def minimalKSum(self, nums: List[int], k: int) -> int:
        book, t = set(nums), count(k + 1)
        return (1 + k) * k // 2 + sum(next(y for y in t if y not in book) - x for x in book if x <= k)

6018. 根据描述创建二叉树

集合+哈希,五行搞定

class Solution:
    def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
        parients,children = set([t[0] for t in descriptions]),set([t[1] for t in descriptions])
        root,nodes = list(parients-children)[0],{c:TreeNode(c) for c in parients|children}
        for p, c, l in descriptions :
            (nodes[p].left,nodes[p].right) = (nodes[c],nodes[p].right) if l==1 else (nodes[p].left,nodes[c])
        return nodes[root]

6019. 替换数组中的非互质数

栈模拟,五行搞定

class Solution:
    def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
        lcm,stack = lambda a,b:a*b // math.gcd(a,b),list()
        for x in nums:
            while stack and math.gcd(stack[-1], x) != 1:x,y = lcm(x,stack[-1]),stack.pop()
            stack.append(x)
        return stack

总结

这次周赛中规中矩,T1+T2+T3+T4共1+2+5+5=13行代码,完成【20行完成周赛】的目标!

  • 41
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 50
    评论
评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可可卷

不要看到我~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值