struct HashNode{
int val;
int index;
UT_hash_handle hh;
};
int cmp(struct HashNode*a,struct HashNode*b){
return b->index-a->index;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* topKFrequent(int* nums, int numsSize, int k, int* returnSize){
struct HashNode* map=NULL;
int i;
(*returnSize)=0;
for(i=0;i<numsSize;i++){
struct HashNode*s=NULL;
HASH_FIND_INT(map,&nums[i],s);
if(s==NULL){
s=malloc(sizeof(struct HashNode));
s->val=nums[i];
s->index=1;
HASH_ADD_INT(map,val,s);
}
else
s->index++;
}
HASH_SORT(map,cmp);
struct HashNode *head=map;
while(k){
nums[(*returnSize)++]=head->val;
head=head->hh.next;
k--;
}
return nums;
}
Leetcode第347题前k个高频元素||C语言(待更新)
最新推荐文章于 2024-08-19 04:53:13 发布