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

阅读 17

2024-09-12

题目:

题解:

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)

0 0 举报