题解 | 计算某字符出现次数
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream> #include <unordered_map> #include <string> using namespace std; int main() { string str; getline(cin,str); unordered_map<char,int>mp; for(char i : str){ mp[i]++; } char target; cin >> target; int ans = 0; if( target >='a' && target <= 'z'){ ans = mp[target] + mp[target - 32]; }else if (target >='A' && target <= 'Z'){ ans = mp[target] + mp[target + 32]; }else { ans = mp[target]; } cout << ans <<endl; return 0; } // 64 位输出请用 printf("%lld")
主要是,ascii表中的大写字母范围和小写字母范围,大写的A是65 ,小写的a是97;两者之间隔着32;