HJ10 题解 | #字符个数统计# bitmap思想
字符个数统计
https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50
#include<iostream> #include<string> using namespace std; int countChar(string str, int len) { int temp = 0; int count[128]; //一个128的数组用值为1或0来标记字符是否出现过 for (int i = 0; i < 128; i++) { count[i] = 0; } for (int i = 0; i < len; i++) { temp=str[i]-'\0'; //字符转数字 if (temp>= 0 && temp <= 127) { count[temp] = 1; //字符对应的位置标记为1; } } temp = 0; for (int i = 0; i < 128; i++) { //统计1的个数即为字符种类 temp += count[i]; } return temp; } int main() { string str; getline(cin, str); int len = str.length(); int charType = countChar(str, len); cout << charType; return 0; }
华为机试刷题实录 文章被收录于专栏
记录一下本科应届生(我自己)刷华为机试题的过程