首页 > 其他 > 详细

【leetcode】相对名次

时间:2020-09-09 20:41:50      阅读:79      评论:0      收藏:0      [点我收藏+]

 

typedef struct _Data{
    int index;
    int val;
} Data;

int cmp(const void *a, const void *b){
    return ((Data*)b)->val - ((Data*)a)->val;
}

char ** findRelativeRanks(int* nums, int numsSize, int* returnSize){
    *returnSize = numsSize;
    if (numsSize == 0) return NULL;
    Data *d = (Data*)malloc(numsSize * sizeof(Data));
    int i;
    for (i = 0; i < numsSize; ++i) {
        d[i].index = i;
        d[i].val = nums[i];
    }
    qsort(d, numsSize, sizeof(Data), cmp);
    char **res = (char**)malloc(numsSize * sizeof(char*));
    memset(res, 0, numsSize * sizeof(char*));
    for (i = 0; i < numsSize; ++i){
        res[d[i].index] = (char*)malloc(13 * sizeof(char));
        memset(res[d[i].index], 0, 13 * sizeof(char));
        switch (i) {
        case 0:
            sprintf(res[d[i].index], "Gold Medal");
            break;
        case 1:
            sprintf(res[d[i].index], "Silver Medal");
            break;
        case 2:
            sprintf(res[d[i].index], "Bronze Medal");
            break;
        default:
            sprintf(res[d[i].index], "%d", i + 1);
            break;
        }
    }
    return res;
}

 

【leetcode】相对名次

原文:https://www.cnblogs.com/ganxiang/p/13641145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!