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周赛最短代码系列每周都会更新,至少覆盖周赛三题,希望大家多多支持!