0
点赞
收藏
分享

微信扫一扫

十一、C语言:字符串函数

书写经典 2024-09-12 阅读 16

题目:

题解:

typedef struct {
    int *nums;
    int numsSize;
} Solution;


Solution* solutionCreate(int* nums, int numsSize) {
    Solution *obj = (Solution *)malloc(sizeof(Solution));
    obj->nums = nums;
    obj->numsSize = numsSize;
    return obj;
}

int solutionPick(Solution* obj, int target) {
    int ans;
    for (int i = 0, cnt = 0; i < obj->numsSize; ++i) {
        if (obj->nums[i] == target) {
            ++cnt; 
            if (rand() % cnt == 0) {
                ans = i;
            }
        }
    }
    return ans;
}

void solutionFree(Solution* obj) {
    free(obj);
}
举报

相关推荐

0 条评论