Short C++ Solution


  • 0
    F
    string reverseStr(string s, int k) {
            if(s.length()<=1) return s;
            for(int i=0, char_rev=k;i<s.length(); i+=2*k){
               if(s.length()-i<k) char_rev=s.length()-i;
               for(int j=i, l=i+char_rev-1; j<l; j++, l--)             
                  swap(s[j],s[l]);
            }
            return s;          
        }
    

Log in to reply
 

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