mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-23 00:30:39 +08:00
添加 剑指Offer 05.替换空格 C语言版本
This commit is contained in:
@ -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:
|
||||||
```Java
|
```Java
|
||||||
|
Reference in New Issue
Block a user