Concise c++ Solution (probably in-place)


  • 0
    L
    string reverseStr(string& s, int k) {
        for(int i=0, n=s.size(); i<n; i += 2*k) reverse(s.begin()+i, s.begin()+min(i+k, n));
        return s;
    }
    

    Or
    string reverseStr(string& s, int k) {
    for(int i=0; i<s.size(); i += 2k) reverse(s.begin()+i, min(s.begin()+i+k, s.end()));
    return s;
    }
    Or
    string reverseStr(string& s, int k) {
    for(int i=0; i<s.size(); i += 2
    k) reverse(s.begin()+i, s.begin()+min(i+k, (int)s.size()));
    return s;
    }

    Same idea : )


Log in to reply
 

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