image-20241108032757459

思路

还是遍历,先遍历k到strlen(s)的,放到res里,之后再把0到k的接在后面就ok了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char* reverseLeftWords(char* s, int n) {
int len = strlen(s);
char* res = (char*)malloc(len + 1);
int index = 0;

for (int i = n; i < len; i++) {
res[index++] = s[i];
}

// 将前 n 个字符添加到 res
for (int i = 0; i < n; i++) {
res[index++] = s[i];
}

res[index] = '\0';
return res;
}