zoukankan      html  css  js  c++  java
  • 【leetcode】按照频率将数组升序排序

    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    typedef struct st{
        int val;
        int cnt;
    }st;
    int cmp(const void* a,const void* b){
        return ((*(st*)a).cnt != (*(st*)b).cnt)?(*(st*)a).cnt-(*(st*)b).cnt :(*(st*)a).val < (*(st*)b).val;
    }
    int* frequencySort(int* nums, int numsSize, int* returnSize){
        st arr[201]={0};
        int* ret = (int*)calloc(101,sizeof(int));
        int i,pst=0,n=0;
        for(i=0; i<numsSize; ++i){
            arr[nums[i]+100].val=nums[i];     
            arr[nums[i]+100].cnt++;
        }
        qsort(arr,201,sizeof(st),cmp);
        for(i=0; i<201; ++i){
            while(arr[i].cnt){
                ret[pst++]=arr[i].val;
                arr[i].cnt--;
            }
        }
        *returnSize=pst;
        return ret;
    }
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    typedef struct st{
        int val;
        int cnt;
    }st;
    int cmp(const void* a,const void* b){
        return ((*(st*)a).cnt != (*(st*)b).cnt)?(*(st*)a).cnt-(*(st*)b).cnt :(*(st*)a).val < (*(st*)b).val;
    }
    int* frequencySort(int* nums, int numsSize, int* returnSize){
        st arr[201]={0};
        int* ret = (int*)calloc(101,sizeof(int));
        int i,pst=0,n=0;
        for(i=0; i<numsSize; ++i){
            arr[nums[i]+100].val=nums[i];     
            arr[nums[i]+100].cnt++;
        }
        qsort(arr,201,sizeof(st),cmp);
        for(i=0; i<201; ++i){
            while(arr[i].cnt){
                ret[pst++]=arr[i].val;
                arr[i].cnt--;
            }
        }
        *returnSize=pst;
        return ret;
    }
  • 相关阅读:
    day19 反射
    Oracle函数整理
    在博客园设置访问人数
    数据库中行转列
    Oracle中数据库与实例的区别
    sql语句的执行顺序
    【地址】ps_cs6安装
    ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
    人员管理模块密码过期
    相关性配置模块总结
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13946660.html
Copyright © 2011-2022 走看看