[544. Output Contest Matches] C++_using iterator_AC


  • 0
    class Solution {
    public:
    string findContestMatch(int n) {
        if(n <= 0 || n%2) return "";
        vector<string> res;
        for(int i = 1, j = n; i <= n/2; ++i, --j){
            res.push_back( '(' + to_string(i) + ',' + to_string(j) + ')');
        }
        while(res.size() > 1){
            int n = res.size(), count = 0;
            auto p = res.begin();
            while(p != res.end()){
                string tmp = '(' + *p + ',' + res.back() + ')';
                res.pop_back();
                *p = tmp;
                p++;
            }
        }
        return res[0];
    }
    };

Log in to reply
 

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