【算法】力扣第 270 场周赛

5942. 找出 3 位偶数

随手一行

class Solution:
    def findEvenNumbers(self, digits: List[int]) -> List[int]:
        return  sorted({x[0]*100+x[1]*10+x[2] for x in permutations(digits,3) if x[0] and not x[2]&1})

5943. 删除链表的中间节点

经典五行

class Solution:
    def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        n,tmp,cur = 1,head,head
        while tmp:=tmp.next:n+=1
        for i in range(n // 2 - 1):cur = cur.next
        cur.next = cur.next.next if n>1 else None
        return head if n>1 else None

5944. 从二叉树一个节点到另一个节点每一步的方向

强行十行

class Solution:
    def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
        self.sp,self.dp,i=None,None,0
        def dfs(root,path):
            if not root:return
            if root.val==startValue:self.sp=path[:]
            if root.val==destValue:self.dp=path[:]
            path.append('L');dfs(root.left,path)
            path[-1]='R';dfs(root.right,path);path.pop()
        dfs(root,[])

        while i<(n:=min(len(self.sp),len(self.dp))) and self.sp[i]==self.dp[i]: i+=1 
        return 'U'*len(self.sp[i:])+''.join(self.dp[i:])

python之禅

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!

人生苦短,我用python!

代码太长,我写一行!

python周赛最短代码系列每周都会更新,至少覆盖周赛三题,希望大家多多支持!

  • 36
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 37
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

可可卷

不要看到我~~

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

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

打赏作者

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

抵扣说明:

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

余额充值