题解 | #图片整理#
图片整理
http://www.nowcoder.com/practice/2de4127fda5e46858aa85d254af43941
不用排序,直接统计每个元素出现的个数即可,因为出现的元素只有'0'~'1', 'A'~'Z', 'a'~'z',共10+26+26=62个,故只需定义一个长度62的数组。不过因为测试数据最长只有1024,实际运行时与其他方法效率上差不太多。
#include<iostream> #include<vector> using namespace std; int main(){ string str; while(getline(cin,str)){ short int count[62] = {0}; // 0-9, A-Z, a-z for(int i=0;i<str.size();++i){ if(str[i]>='a'){ ++count[str[i]-'a'+36]; }else if(str[i]>='A'){ ++count[str[i]-'A'+10]; }else{ ++count[str[i]-'0']; } } string sortStr; for(int i=0;i<62;++i){ if(count[i]>0){ if(i<10) // 0~9 sortStr.append(count[i],'0'+i); else if(i<36) //'A'~'Z' sortStr.append(count[i],i+55); // int('A')=65 else //'a'~'z' sortStr.append(count[i],i+61); // int('a')=97 } } cout<<sortStr<<endl; } return 0; }