From 292fe1eb380622ce4fc19f2d05ef26bb472e855d Mon Sep 17 00:00:00 2001 From: hs-zhangsan <1513157458@qq.com> Date: Sat, 19 Feb 2022 15:55:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87Offer=20?= =?UTF-8?q?05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer05.替换空格.md | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 530545fb..9e743887 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -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