0390.cpp (249B)
1 class Solution { 2 public: 3 int lastRemaining(int n) const { 4 int head = 1; 5 6 for (int step = 1, dir = 1; n > 1; step *= 2, n /= 2, dir = !dir) { 7 head += dir || n % 2 ? step : 0; 8 } 9 10 return head; 11 } 12 };