【每日刷题】Day36
🥕个人主页:开敲🍉
🔥所属专栏:每日刷题🍍
🌼文章目录🌼
1. 232. 用栈实现队列 - 力扣(LeetCode)
2. 2960. 统计已测试设备 - 力扣(LeetCode)
3. 1700. 无法吃午餐的学生数量 - 力扣(LeetCode)
1. 232. 用栈实现队列 - 力扣(LeetCode)
//栈和队列的巩固题。本题思路仅供参考。
typedef int STDataType;
//栈结构体
typedef struct Stack
{
STDataType* arr;
int top;
int capacity;
int flag;
}ST;
//初始化栈
void StackInit(ST* st)
{
assert(st);
st->arr = NULL;
st->capacity = st->top = 0;
}
//释放栈
void StackRelease(ST* st)
{
assert(st);
free(st->arr);
st->arr = NULL;
st->capacity = st->top = 0;
st->flag = -1;
}
//判空
bool StackEmpty(ST* st)
{
assert(st);
return st->top == 0;
}
//入栈
void StackPush(ST* st, STDataType x)
{
assert(st);
if (st->top == st->capacity)
{
int newcapacity = st->capacity == 0 ? 4 : 2 * st->capacity;
STDataType* tmp = (STDataType*)realloc(st->arr,sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc:");
exit(-1);
}
st->arr = tmp;
st->capacity = newcapacity;
}
st->arr[st->top] = x;
st->top++;
}
//出栈
void StackPop(ST* st)
{
assert(st);
assert(st->top > 0);
st->top--;
}
//栈顶
STDataType StackTop(ST* st)
{
assert(st);
assert(st->top > 0);
return st->arr[st->top - 1];
}
typedef struct
{
ST st1;
ST st2;
} MyQueue;
//创建队列
MyQueue* myQueueCreate()
{
MyQueue* qe = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&(qe->st1));
StackInit(&(qe->st2));
qe->st1.flag = 1;
return qe;
}
//入列
void myQueuePush(MyQueue* obj, int x)
{
//空
ST* Empty = &(obj->st1);
//非空
ST* NotEmpty = &(obj->st2);
if(!StackEmpty(&(obj->st1)))
{
Empty = &(obj->st2);
NotEmpty = &(obj->st1);
}
while(NotEmpty->top&&(obj->st1.flag == 0))
{
StackPush(Empty,StackTop(NotEmpty));
StackPop(NotEmpty);
}
if(obj->st1.flag==0)
{
StackPush(Empty,x);
}
else
{
StackPush(NotEmpty,x);
}
obj->st1.flag = 1;
}
//出列
int myQueuePop(MyQueue* obj)
{
ST* Empty = &(obj->st1);
ST* NotEmpty = &(obj->st2);
if(!StackEmpty(&(obj->st1)))
{
Empty = &(obj->st2);
NotEmpty = &(obj->st1);
}
while(NotEmpty->top&&(obj->st1.flag == 1))
{
StackPush(Empty,StackTop(NotEmpty));
StackPop(NotEmpty);
}
int ret = 0;
if(obj->st1.flag)
{
ret = StackTop(Empty);
StackPop(Empty);
}
else
{
ret = StackTop(NotEmpty);
StackPop(NotEmpty);
}
obj->st1.flag = 0;
return ret;
}
//列头元素
int myQueuePeek(MyQueue* obj)
{
ST* Empty = &(obj->st1);
ST* NotEmpty = &(obj->st2);
if(!StackEmpty(&(obj->st1)))
{
Empty = &(obj->st2);
NotEmpty = &(obj->st1);
}
while(NotEmpty->top&&(obj->st1.flag == 1))
{
StackPush(Empty,StackTop(NotEmpty));
StackPop(NotEmpty);
}
int ret = 0;
if(obj->st1.flag)
{
ret = StackTop(Empty);
}
else
{
ret = StackTop(NotEmpty);
}
obj->st1.flag = 0;
return ret;
}
//判空
bool myQueueEmpty(MyQueue* obj)
{
return StackEmpty(&(obj->st1))&&StackEmpty(&(obj->st2));
}
//释放队列
void myQueueFree(MyQueue* obj)
{
StackRelease(&obj->st1);
StackRelease(&obj->st2);
free(obj);
obj = NULL;
}
2. 2960. 统计已测试设备 - 力扣(LeetCode)
//思路:遍历数组。记录当前所有设备的电池需要降低的百分比,将当前元素大于需要降低的百分比时,需要降低的百分比++
int countTestedDevices(int* batteryPercentages, int batteryPercentagesSize)
{
int count = 0;
int i = 0;
if(batteryPercentages[0]>=1)//判断数组中第一个元素是否为0,不为0则将所有设备的电池需要降低的百分比++,同时将i置为1,从数组第二个元素开始遍历
{
count++;
i++;
}
for(;i<batteryPercentagesSize;i++)
{
if(batteryPercentages[i]>count)//如果当前元素>需要降低的百分比,需要降低的百分比++
{
count++;
}
}
return count;
}
3. 1700. 无法吃午餐的学生数量 - 力扣(LeetCode)
//0ms 100%思路:队列。使用两个队列qe1和qe2分别存放students和sandwiches数组,随后比较两个队列的队头元素,如果相同,studentsSize--,否则,将qe1队列的队头元素先存储起来,随后将其删除,随后再将其存入队尾,实现该同学回到队尾。随后继续重复上述流程。注意:这里的队头元素比较为循环,因此需要一个标志来判断循环的结束与否,这里的思路是定义一个变量,该变量与qe1队列中的元素个数比较,该变量大于qe1队列中元素个数时,跳出循环。
//以下为队列的实现函数
typedef int QDataType;
//队列节点
typedef struct listnode
{
QDataType val;
struct listnode* next;
}LN;
//队列头尾指针
typedef struct Queque
{
LN* phead;
LN* ptail;
int size;
}QE;
//队列初始化
void QueInit(QE* qe)
{
assert(qe);
qe->phead = NULL;
qe->ptail = NULL;
qe->size = 0;
}
//入列
void QuePush(QE* qe, QDataType x)
{
assert(qe);
LN* newnode = (LN*)malloc(sizeof(LN));
if (newnode == NULL)
{
perror("malloc:");
exit(-1);
}
newnode->next = NULL;
newnode->val = x;
if (qe->phead == NULL)
{
qe->phead = qe->ptail = newnode;
}
else
{
qe->ptail->next = newnode;
qe->ptail = qe->ptail->next;
}
qe->size++;
}
//出列
void QuePop(QE* qe)
{
assert(qe);
assert(qe->phead != NULL);
assert(qe->size > 0);
LN* tmp = qe->phead->next;
free(qe->phead);
qe->phead = tmp;
qe->size--;
}
//获取列头元素
QDataType QueGetHead(QE* qe)
{
assert(qe);
assert(qe->phead != NULL);
return qe->phead->val;
}
//获取列尾元素
QDataType QueGetBack(QE* qe)
{
assert(qe);
assert(qe->ptail != NULL);
return qe->ptail->val;
}
//获取队列元素个数
int QueSize(QE* qe)
{
assert(qe);
return qe->size;
}
//判断队列是否为空
bool QueEmpty(QE* qe)
{
assert(qe);
return qe->size == 0;
}
//释放队列
void QueRelease(QE* qe)
{
assert(qe);
while (qe->phead)
{
LN* tmp = qe->phead->next;
free(qe->phead);
qe->phead = tmp;
}
qe->ptail = NULL;
qe->size = 0;
free(qe);
qe = NULL;
}
//以上为队列实现函数,下面直接调用接口函数
int countStudents(int* students, int studentsSize, int* sandwiches, int sandwichesSize)
{
QE qe1;
QE qe2;
QueInit(&qe1);
QueInit(&qe2);
for(int i = 0;i<studentsSize;i++)//将students和sandwiches数组存入队列
{
QuePush(&qe1,students[i]);
QuePush(&qe2,sandwiches[i]);
}
int count = studentsSize;//count为吃不上饭的学生人数,初始为学生总人数
int flag = 0;//用于下面循环结束与否的判断条件
while(flag<qe1.size)
{
if(QueGetHead(&qe1)==QueGetHead(&qe2))//如果当前学生遇到了喜欢的三明治,吃不上饭的学生(count)--,并将flag重新置为0
{
QuePop(&qe1);
QuePop(&qe2);
flag = 0;
count--;
}
else//如果当前学生遇到的不是喜欢的三明治,将学生置于队尾,flag++
{
int tmp = QueGetHead(&qe1);
QuePop(&qe1);
QuePush(&qe1,tmp);
flag++;
}
}
return count;
}