10-line C++ Iterative Solution


  • 0
    class Solution {
    public:
        string findContestMatch(int n) {
            vector<string> pass;
            for(int i=0;i<n/2;i++)
                pass.push_back("("+to_string(i+1)+","+to_string(n-i)+")");
            while(pass.size()>1) {
                vector<string> pass_tmp;
                for(int i=0;i<pass.size()/2;i++)
                    pass_tmp.push_back("("+pass[i]+","+pass[pass.size()-1-i]+")");
                pass = pass_tmp;
            }
            return pass[0];
        }
    };
    

Log in to reply
 

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