C++ 4 lines - Problem 2


  • 1
    A
    class Solution {
    public:
        int lastRemaining(int n) {
            if (n == 1) return 1;
            n = n % 2 ? n - 1 : n;
            if (n == 2) return 2;
            return n % 4 == 0 ? 4*lastRemaining(n/4) - 2:4*lastRemaining(n/4);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.