typedef struct {
int *arr;
int len;
int cnt;
} Solution;
Solution* solutionCreate(int* w, int wSize) {
Solution* obj = (Solution*)malloc(sizeof(Solution));
obj->arr = w;
obj->len = wSize;
int i, cnt=0;
for(i=0; i<wSize; i++){
cnt += w[i];
w[i] = cnt;
}
obj->cnt = cnt;
return obj;
}
int solutionPickIndex(Solution* obj) {
int rd = rand() % obj->cnt;
int i=0;
for(i=0; i<obj->len; i++){
if(obj->arr[i] > rd ) {
return i;
}
}
return 88;
}
void solutionFree(Solution* obj) {
free(obj);
}
/**
* Your Solution struct will be instantiated and called as such:
* Solution* obj = solutionCreate(w, wSize);
* int param_1 = solutionPickIndex(obj);
* solutionFree(obj);
*/
leetcode-528. 按权重随机选择-C语言
最新推荐文章于 2022-10-08 13:05:19 发布