要理解优化的作用,缩进是否减少了判断?
2410. 运动员和训练师的最大匹配数
class Solution:
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
players.sort(reverse=True)
trainers.sort(reverse=True)
k=len(trainers)
i=ans=0
for v in players:
if v <=trainers[i]:
ans+=1
i+=1
if i==k:
return k
return ans
#下面的if 又缩进了
class Solution:
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
players.sort(reverse=True)
trainers.sort(reverse=True)
k=len(trainers)
i=ans=0
for v in players:
if v <=trainers[i]:
ans+=1
i+=1
if i==k:
return k
return ans
网友评论