arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr = [2, 4, 6, 8]
arr = [2, 6]
arr = [6]
//每次都是等差数列
//每次个数n /= 2
//每次d *= 2
//总共四种情况,个数为奇/偶,从左/右开始,只有偶&&从右开始时候a0不变,剩下都是a0 + d
class Solution {
public:
int lastRemaining(int n) {
int num_sum = n;
int a0 = 1;
int d = 1;
int left_or_right_begin = 1; //1表示从左开始
while(num_sum != 1){
if(left_or_right_begin == -1 && num_sum % 2 == 0){
a0 = a0;
}else{
a0 += d;
}
num_sum /= 2;
d *= 2;
left_or_right_begin *= -1;
}
return a0;
}
};