Naive C++ algorithm


  • 0
    J

    At first I was trying to find the rules for arrangement of numbers, but finally, I found the naive method works pretty clean and simple.

    string findContestMatch(int n) {
            vector<string> res(n+1);
            for(int i = 0; i <= n; ++i) res[i] = to_string(i);
            while(n){
                int left = 1, right  = n;
                while(left < right){
                    res[left] = '(' + res[left] + ',' + res[right] + ')';
                    left++;
                    right--;
                }
                n >>= 1;
            }
            return res[1];
        }
    

Log in to reply
 

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