版权声明:版权归属tangobravo所有 https://blog.csdn.net/tangobravo/article/details/85845594
删除字符串中指定的字符。 例如 “abcdaefaghiagkl“ 删除‘a’,以后: “bcdefghigkl”
此算法仅需要一次遍历,只有O(n)的时间复杂度
我原先的算法需要O(n^2)的时间复杂度
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
void delete_char()
{
char c;
char ch[MAX];
char *p, *q;
while (rewind(stdin), gets_s(ch, MAX) != NULL && (c = getchar()) != EOF)
{
p =q= ch;
while (*p)
{
if (*p != c)
{
*q++ = *p;
}
++p;
}
*q = 0;
puts(ch);
/*char *temp;
while (*p)
{
if (*p == c)
{
temp = p;
while (*temp)
{
*temp = *(temp + 1);
++temp;
}
}
++p;
}双层嵌套,O(n^2)的时间复杂度*/
}
}
int main()
{
delete_char();
system("pause");
}