
链接: 6180. 最小偶倍数

。
定级Easy。
就是lcm,最小公倍数。
由于是和2取最小公倍:奇数的是2*x,偶数是x本身。
或者用库函数lcm也可以。
class Solution:
def smallestEvenMultiple(self, n: int) -> int:
if n&1==0:
return n
else:
return n*2

定级Medium。
class Solution:
def longestContinuousSubstring(self, s: str) -> int:
n = len(s)
f = [1]*n
for i in range(1,n):
if ord(s[i])- ord(s[i-1]) == 1:
f[i] = f[i-1]+1
return max(f)
链接: 6182. 反转二叉树的奇数层

定级Medium。
class Solution:
def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return root
q = [root]
depth = 0
while q:
nq = []
for u in q:
if u.left:
nq.append(u.left)
if u.right:
nq.append(u.right)
if depth & 1:
n = len(q)
for i in range(n//2):
q[i].val, q[n-i-1].val = q[n-i-1].val, q[i].val
depth += 1
q = nq
return root
链接: 6183. 字符串的前缀分数和

定级Hard。
class Solution:
def sumPrefixScores(self, words: List[str]) -> List[int]:
trie = {}
for s in words:
pos = trie
for c in s:
if c not in pos:
pos[c] = {}
pos[c]['cnt'] = 0
pos = pos[c]
pos['cnt'] += 1
pos['end'] = s
# print(trie)
ans = []
def find(s):
pos = trie
ret = 0
for c in s:
if c not in pos:
return ret
ret += pos[c]['cnt']
pos = pos[c]
# if 'end' in pos:
# return
return ret
for s in words:
ans.append(find(s))
return ans