题目描述
编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。
输入描述:
输入N个字符,字符在ACSII码范围内。
输出描述:
输出范围在(0~127)字符的个数。
示例1
输入
abc
输出
3
问题地址:https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50?tpId=37&tqId=21233&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking
思路:ascii范围在0~127,则利用一个长度为128的数组记录是否重复。判断重复经常用数组。3ms运行时间
#include <iostream>
using namespace std;
int main()
{
string str;
while(cin >> str)
{
int count = 0;
int flag[128] = {0};
for(char c : str)
{
if(c >=0 && c <= 127)
if(!flag[c])
{
count++;
flag[c] = 1;
}
}
cout << count << endl;;
}
return 0;
}