题解 | 【C语言】#字符个数统计#
字符个数统计
https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50
#include <stdio.h>
#include <string.h>
int cmp(const void* p1, const void* p2)
{
return *(char*)p1 - *(char*)p2;
}
int main()
{
char str[500] = {0};
scanf("%s", str);
qsort(str, strlen(str), sizeof(char), cmp);
int count = 1;
char pre = str[0];
for(int i = 1; i < strlen(str); i++)
{
if(str[i] != pre)
{
count++;
pre = str[i];
}
}
printf("%d", count);
return 0;
}
先用快排排序,再遍历找到不同的字符个数
快排的时间复杂度:NlogN,空间复杂度:logN
#互联网没坑了,还能去哪里?#

