C++ two pointers one pass O(n) solution


  • 0

    Time complexity: O((n/2k)*2k)=O(n).

        string reverseStr(string s, int k) {
            string::iterator head=s.begin();
            string::iterator tail=head;
            while(tail!=s.end()){
                while(tail-head<k&&tail!=s.end()) tail++;
                string::iterator t1=head;
                string::iterator t2=tail;
                reverse(t1,t2);
                head=tail;
                while(tail-head<k&&tail!=s.end()) tail++;
                head=tail;
            }
            return s;
        }
    

Log in to reply
 

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