public class Solution {
public int lastRemaining(int n) {
int step = 1;
int head = 1;
boolean left = true;
while (n > 1) {
if (left || n % 2 == 1) {
// 需要把head前移
head += step;
}
left = ! left;
n /= 2;
step *= 2;
}
return head;
}
}