看数据范围,直接一行
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)]
贪心,等差数列求和,三行解法👇
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)
集合+哈希,五行搞定
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]
栈模拟,五行搞定
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行完成周赛】的目标!
|