点击打开链接
class Solution {
public:
int lastRemaining(int n) {
int remaining = n, head = 1, step = 1;
bool isLeft = true;
while (remaining>1) {
if(isLeft || (remaining&1)==1){
head = head+step;
}
step *=2;
remaining /= 2;
isLeft = !isLeft;
}
return head;
}
};