My C++ Solution - 10 ms


  • 0
    D
    string reverseStr(string s, int k) {
    	int i1, i = 0;
    	char ch;
    	while (i < s.size()) {
    		i1 = 0;
    		if (i + k <= s.size()) {
    			for (int j = i; j < (2 * i + k) / 2; j++) {
    				ch = s[j];
    				s[j] = s[i + k - 1 - i1];
    				s[i + k - 1 - (i1++)] = ch;
    			}
    		}
    		else {
    			for (int j = i; j < (s.size() + i) / 2; j++) {
    				ch = s[j];
    				s[j] = s[s.size() - 1 - i1];
    				s[s.size() - 1 - (i1++)] = ch;
    			}
    		}
    		i += 2 * k;
    	}
    	return s;
    }
    

Log in to reply
 

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