生日第一件事 核酸
生日第二件事 吃饭
生日第三件事 每日一题
java:
官方题解思路(下面截图)谢谢~等差数列模拟
class Solution {
public int lastRemaining(int n) {
int first = 1, end = n;
int sum = n, k = 0, step = 1;
while(sum > 1){
if (k % 2 == 0) {
first = first + step;
end = (sum % 2 == 0) ? end : end - step;
} else {
first = (sum % 2 == 0) ? first : first + step;
end = end-step;
}
k ++;
sum = sum >> 1;
step = step << 1;
}
return first;
}
}
python3:
class Solution:
def lastRemaining(self, n: int) -> int:
a1 = 1
k, cnt, step = 0, n, 1
while cnt > 1:
if k % 2 == 0:
a1 += step
else:
if cnt % 2:
a1 += step
k += 1
cnt >>= 1
step <<= 1
return a1



