Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C - 2336. Smallest Number in Infinite Set
題目:

解答:
#define MAX_SIZE 1000
typedef struct {
int cnt[MAX_SIZE];
} SmallestInfiniteSet;
SmallestInfiniteSet* smallestInfiniteSetCreate()
{
int i = 0;
SmallestInfiniteSet *r = malloc(sizeof(SmallestInfiniteSet));
for (i = 0; i < MAX_SIZE; i++) {
r->cnt[i] = 1;
}
return r;
}
int smallestInfiniteSetPopSmallest(SmallestInfiniteSet *obj)
{
int i = 0;
for (i = 0; i < MAX_SIZE; i++) {
if (obj->cnt[i]) {
obj->cnt[i] = 0;
return i + 1;
}
}
return 0;
}
void smallestInfiniteSetAddBack(SmallestInfiniteSet *obj, int num)
{
obj->cnt[num - 1] = 1;
}
void smallestInfiniteSetFree(SmallestInfiniteSet *obj)
{
free(obj);
}
/**
* Your SmallestInfiniteSet struct will be instantiated and called as such:
* SmallestInfiniteSet* obj = smallestInfiniteSetCreate();
* int param_1 = smallestInfiniteSetPopSmallest(obj);
* smallestInfiniteSetAddBack(obj, num);
* smallestInfiniteSetFree(obj);
*/