【C语言Coding】第二十一天
原创
©著作权归作者所有:来自51CTO博客作者小样小响的原创作品,请联系作者获取转载授权,否则将追究法律责任
typedef struct {
int head;
int tail;
int data[10001];
} RecentCounter;
RecentCounter* recentCounterCreate() {
RecentCounter* obj = (RecentCounter*)malloc(sizeof(RecentCounter));
obj->head = 0;
obj->tail = -1;
return obj;
}
int recentCounterPing(RecentCounter* obj, int t) {
obj->data[ ++obj->tail ] = t;
while(t - obj->data[obj->head] > 3000 ){
++obj->head;
}
return obj->tail - obj->head + 1;
}
void recentCounterFree(RecentCounter* obj) {
free(obj);
}
/**
* Your RecentCounter struct will be instantiated and called as such:
* RecentCounter* obj = recentCounterCreate();
* int param_1 = recentCounterPing(obj, t);
* recentCounterFree(obj);
*/
int countStudents(int* students, int studentsSize, int* sandwiches, int sandwichesSize){
int* arr = (int*)malloc(sizeof(int)*30000);
int L = 0;
int R = -1;
int loop = 0;
for(int i=0;i<studentsSize;i++){
arr[++R] = students[i];
}
// 将学生作为一个队列
int cur = 0; // 标记三明治索引
while(L<=R){
if(arr[L] == sandwiches[cur]){
L++;
cur++;
if(cur == sandwichesSize) return 0;
}else{
arr[++R] = arr[L];
L++;
if(loop++ > 500) return R-L+1;
}
}
return R-L+1;
}
// 定义一个栈
struct stack {
int data[10000];
int cur;
};
// 栈的插入操作
void push(struct stack* obj,int val){
obj->data[++obj->cur] = val;
}
// 栈的删除
void pop(struct stack* obj){
obj->cur--;
}
// 栈顶
int gtop(struct stack* obj){
return obj->data[obj->cur];
}
// 用两个栈实现的队列
typedef struct {
struct stack* stk1;
struct stack* stk2;
} MyQueue;
/** Initialize your data structure here. */
// 队列初始化
MyQueue* myQueueCreate() {
MyQueue* mq = (MyQueue*)malloc(sizeof(MyQueue));
mq->stk1 = (struct stack*)malloc(sizeof(struct stack));
mq->stk1->cur = -1;
mq->stk2 = (struct stack*)malloc(sizeof(struct stack));
mq->stk2->cur = -1;
return mq;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
push(obj->stk1,x);
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
// 倒出
while(obj->stk1->cur >= 0){
push(obj->stk2,gtop(obj->stk1));
pop(obj->stk1);
}
int ans = gtop(obj->stk2);
pop(obj->stk2);
// 倒回
while(obj->stk2->cur >= 0){
push(obj->stk1,gtop(obj->stk2));
pop(obj->stk2);
}
return ans;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
while(obj->stk1->cur >= 0){
push(obj->stk2,gtop(obj->stk1));
pop(obj->stk1);
}
int val = gtop(obj->stk2);
while(obj->stk2->cur >= 0){
push(obj->stk1,gtop(obj->stk2));
pop(obj->stk2);
}
return val;
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
if(obj->stk1->cur >= 0)
return false;
else return true;
}
/** free */
void myQueueFree(MyQueue* obj) {
free(obj);
}
/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);
* int param_2 = myQueuePop(obj);
* int param_3 = myQueuePeek(obj);
* bool param_4 = myQueueEmpty(obj);
* myQueueFree(obj);
*/
typedef struct {
int data[20000];
int tail;
int head;
} CQueue;
CQueue* cQueueCreate() {
CQueue* q = (CQueue*)malloc(sizeof(CQueue));
q->tail = -1;
q->head = 0;
return q;
}
void cQueueAppendTail(CQueue* obj, int value) {
obj->data[++obj->tail] = value;
}
int cQueueDeleteHead(CQueue* obj) {
if(obj->head != obj->tail + 1)
return obj->data[obj->head++];
else
return -1;
}
void cQueueFree(CQueue* obj) {
free(obj);
}
/**
* Your CQueue struct will be instantiated and called as such:
* CQueue* obj = cQueueCreate();
* cQueueAppendTail(obj, value);
* int param_2 = cQueueDeleteHead(obj);
* cQueueFree(obj);
*/
typedef struct stack{
int data[10000];
int size;
}Stack;
void push(Stack* stk,int val){
stk->data[++stk->size] = val;
}
void pop(Stack* stk){
stk->size--;
}
int gtop(Stack* stk){
return stk->data[stk->size];
}
typedef struct {
Stack* stk1;
Stack* stk2;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* mq = (MyQueue*)malloc(sizeof(MyQueue));
mq->stk1 = (Stack*)malloc(sizeof(Stack));
mq->stk1->size = -1;
mq->stk2 = (Stack*)malloc(sizeof(Stack));
mq->stk2->size = -1;
return mq;
}
void myQueuePush(MyQueue* obj, int x) {
push(obj->stk1,x);
}
int myQueuePop(MyQueue* obj) {
while(obj->stk1->size >= 0){
push(obj->stk2,gtop(obj->stk1));
pop(obj->stk1);
}
int val = gtop(obj->stk2);
pop(obj->stk2);
while(obj->stk2->size >=0){
push(obj->stk1,gtop(obj->stk2));
pop(obj->stk2);
}
return val;
}
int myQueuePeek(MyQueue* obj) {
while(obj->stk1->size >= 0){
push(obj->stk2,gtop(obj->stk1));
pop(obj->stk1);
}
int val = gtop(obj->stk2);
while(obj->stk2->size >=0){
push(obj->stk1,gtop(obj->stk2));
pop(obj->stk2);
}
return val;
}
bool myQueueEmpty(MyQueue* obj) {
if(obj->stk1->size >= 0){
return false;
}else{
return true;
}
}
void myQueueFree(MyQueue* obj) {
free(obj);
}
/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);
* int param_2 = myQueuePop(obj);
* int param_3 = myQueuePeek(obj);
* bool param_4 = myQueueEmpty(obj);
* myQueueFree(obj);
*/