解题思路:
除了模拟。没有任何想法,脑子属实是干的
看来题解,发现模拟也不行,时间复杂度超了
仔细研究了官方给的题解,自己复述了一遍:
class Solution {
public:
int lastRemaining(int n) {
int a1 = 1, an = n;
int k = 0, cnt = n, step = 1;
while (cnt > 1) {
if (k % 2 == 1) {
a1 = (cnt % 2 == 1) ? a1 + step: a1;
an = an - step;
} else {
a1 = a1 + step;
an = (cnt % 2 == 1) ? an - step: an;
}
++k;
cnt = cnt>>1;
step = step<<1;
}
return a1;
}
};