char * frequencySort(char * s){ int len = strlen(s); int hash[128]={0}; for(int i=0;i<len;i++){ hash[s[i]]++; } int cmp(const void* a,const void* b){ char pa = *(char* )a; char pb = *(char* )b; int len1 = hash[pa]; int len2 = hash[pb]; if(len1==len2){ //当次数相等的时候,排序字符 return pa-pb; //字符 ASCII大小降序 } return len2-len1; //字符出现次数大小 从小到大 } qsort(s,len,sizeof(char),cmp); return s; } |