mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 21:50:49 +08:00
@ -98,8 +98,31 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
C:
|
||||
|
||||
```c
|
||||
char * reverseStr(char * s, int k){
|
||||
int len = strlen(s);
|
||||
|
||||
for (int i = 0; i < len; i += (2 * k)) {
|
||||
//判断剩余字符是否少于 k
|
||||
k = i + k > len ? len - i : k;
|
||||
|
||||
int left = i;
|
||||
int right = i + k - 1;
|
||||
while (left < right) {
|
||||
char temp = s[left];
|
||||
s[left++] = s[right];
|
||||
s[right--] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
```
|
||||
|
||||
Java:
|
||||
|
||||
```Java
|
||||
//解法一
|
||||
class Solution {
|
||||
|
@ -121,6 +121,37 @@ for (int i = 0; i < a.size(); i++) {
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
C:
|
||||
```C
|
||||
char* replaceSpace(char* s){
|
||||
//统计空格数量
|
||||
int count = 0;
|
||||
int len = strlen(s);
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (s[i] == ' ') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
//为新数组分配空间
|
||||
int newLen = len + count * 2;
|
||||
char* result = malloc(sizeof(char) * newLen + 1);
|
||||
//填充新数组并替换空格
|
||||
for (int i = len - 1, j = newLen - 1; i >= 0; i--, j--) {
|
||||
if (s[i] != ' ') {
|
||||
result[j] = s[i];
|
||||
} else {
|
||||
result[j--] = '0';
|
||||
result[j--] = '2';
|
||||
result[j] = '%';
|
||||
}
|
||||
}
|
||||
result[newLen] = '\0';
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Java:
|
||||
```Java
|
||||
|
Reference in New Issue
Block a user