题解 | #字符个数统计#哈希-数组
字符个数统计
https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50
#include <iostream>
#include <string>
using namespace std;
int caculateNum(string s) {
int hash[128] = {0};
int num = 0;
for (int i = 0; i < s.size(); i++) {
//确保输入的字符ASCII 码范围在0-127
if (int(s[i]) >= 0 && int(s[i]) <= 127)
hash[int(s[i])] = 1;
}
for (int i = 0; i < 128; i++) {
if (hash[i] == 1) {
num += 1;
}
}
return num;
}
int main() {
string n;
while (cin >> n) { // 注意 while 处理多个 case
cout << caculateNum(n) << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")