Bootstrap

C语言 | Leetcode C语言题解之第541题反转字符串II

题目:

题解:

void swap(char* a, char* b) {
    char tmp = *a;
    *a = *b, *b = tmp;
}

void reverse(char* l, char* r) {
    while (l < r) {
        swap(l++, --r);
    }
}

int min(int a, int b) {
    return a < b ? a : b;
}

char* reverseStr(char* s, int k) {
    int n = strlen(s);
    for (int i = 0; i < n; i += 2 * k) {
        reverse(&s[i], &s[min(i + k, n)]);
    }
    return s;
}
;