0
点赞
收藏
分享

微信扫一扫

1688. 比赛中的配对次数

1688. 比赛中的配对次数_算法
‘’’
思路:不断用n整除2得到res 直到n变为1 叠加所有res
‘’’

class Solution:
def numberOfMatches(self, n: int) -> int:
'''
思路:不断用n整除2得到res 直到n变为1 叠加所有res
'''
res = 0
while (n != 1):
res += n // 2
n = n / 2 if n % 2 == 0 else n // 2 + 1
return int(res)


举报

相关推荐

0 条评论