Simple C++ Solution


  • 0
    B
    string reverseStr(string s, int k) {
            int n = s.size();
            int i = 0;
            while(1){
                
                if(2*k + i <= n){
                    reverse(s.begin() +i, s.begin()+ i + k);
                    i += 2*k;
                }
                else if(i+k <= n){
                    reverse(s.begin() +i, s.begin() + i + k);
                    return s;
                }
                else if( i + k > n){
                    reverse(s.begin()+i,s.end());
                    return s;
                }
            }
    

Log in to reply
 

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