比赛中的配对次数
力扣链接
解题思路
官方题解
代码(模拟)
//模拟
public int numberOfMatches(int n) {
int count = 0;
while (n > 1) {
if ((n & 1) == 1) {
count += (n - 1) / 2;
n -= (n - 1) / 2;
} else {
count += n / 2;
n /= 2;
}
}
return count;
}
代码(数学)
public int numberOfMatches(int n) {
return n - 1;
}