C++ Simple Iterate and Reverse, O(N) Time O(1) Space


  • 0
    M
    class Solution {
    public:
        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;
        }
    };
    

Log in to reply
 

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