【LeetCode刷题日记】队列类题目常见题型_leetcode 队列 题

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Golang全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注go)
img

正文

stack2.pop();
return deleteItem;
}
}
};

239. 滑动窗口最大值

在这里插入图片描述
在这里插入图片描述

void swap(int** a, int** b) {
int* tmp = *a;
*a = *b, *b = tmp;
}

int cmp(int* a, int* b) {
return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0];
}

struct Heap {
int** heap;
int size;
int capacity;
};

void init(struct Heap* obj, int capacity) {
obj->size = 0;
obj->heap = NULL;
obj->capacity = capacity;
obj->heap = malloc(sizeof(int*) * (obj->capacity + 1));
for (int i = 1; i <= obj->capacity; i++) {
obj->heap[i] = malloc(sizeof(int) * 2);
}
}

void setFree(struct Heap* obj) {
for (int i = 1; i <= obj->capacity; i++) {
free(obj->heap[i]);
}
free(obj->heap);
free(obj);
}

void push(struct Heap* obj, int num0, int num1) {
int sub1 = ++(obj->size), sub2 = sub1 >> 1;
(obj->heap[sub1])[0] = num0, (obj->heap[sub1])[1] = num1;
while (sub2 > 0 && cmp(obj->heap[sub2], obj->heap[sub1]) < 0) {
swap(&(obj->heap[sub1]), &(obj->heap[sub2]));
sub1 = sub2, sub2 = sub1 >> 1;
}
}

void pop(struct Heap* obj) {
int sub = 1;
swap(&(obj->heap[sub]), &(obj->heap[(obj->size)–]));
while (sub <= obj->size) {
int sub1 = sub << 1, sub2 = sub << 1 | 1;
int maxSub = sub;
if (sub1 <= obj->size && cmp(obj->heap[maxSub], obj->heap[sub1]) < 0) {
maxSub = sub1;
}
if (sub2 <= obj->size && cmp(obj->heap[maxSub], obj->heap[sub2]) < 0) {
maxSub = sub2;
}
if (sub == maxSub) {
break;
}
swap(&(obj->heap[sub]), &(obj->heap[maxSub]));
sub = maxSub;
}
}

int* top(struct Heap* obj) {
return obj->heap[1];
}

int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
struct Heap* q = malloc(sizeof(struct Heap));
init(q, numsSize);
for (int i = 0; i < k; i++) {
push(q, nums[i], i);
}
int* ans = malloc(sizeof(int) * (numsSize - k + 1));
*returnSize = 0;
ans[(*returnSize)++] = top(q)[0];

for (int i = k; i < numsSize; ++i) {
push(q, nums[i], i);
while (top(q)[1] <= i - k) {
pop(q);
}
ans[(*returnSize)++] = top(q)[0];
}
setFree(q);
return ans;
}


class Solution {
public:
vector maxSlidingWindow(vector& nums, int k) {
int n = nums.size();
priority_queue<pair<int, int>> q;
for (int i = 0; i < k; ++i) {
q.emplace(nums[i], i);
}
vector ans = {q.top().first};
for (int i = k; i < n; ++i) {
q.emplace(nums[i], i);
while (q.top().second <= i - k) {
q.pop();
}
ans.push_back(q.top().first);
}
return ans;
}
};

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906132327420.png

int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
int q[numsSize];
int left = 0, right = 0;
for (int i = 0; i < k; ++i) {
while (left < right && nums[i] >= nums[q[right - 1]]) {
right–;
}
q[right++] = i;
}
*returnSize = 0;
int* ans = malloc(sizeof(int) * (numsSize - k + 1));
ans[(*returnSize)++] = nums[q[left]];
for (int i = k; i < numsSize; ++i) {
while (left < right && nums[i] >= nums[q[right - 1]]) {
right–;
}
q[right++] = i;
while (q[left] <= i - k) {
left++;
}
ans[(*returnSize)++] = nums[q[left]];
}
return ans;
}


class Solution {
public:
vector maxSlidingWindow(vector& nums, int k) {
int n = nums.size();
deque q;
for (int i = 0; i < k; ++i) {
while (!q.empty() && nums[i] >= nums[q.back()]) {
q.pop_back();
}
q.push_back(i);
}

vector ans = {nums[q.front()]};
for (int i = k; i < n; ++i) {
while (!q.empty() && nums[i] >= nums[q.back()]) {
q.pop_back();
}
q.push_back(i);
while (q.front() <= i - k) {
q.pop_front();
}
ans.push_back(nums[q.front()]);
}
return ans;
}
};

622. 设计循环队列

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906132746631.png

在这里插入图片描述
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906132844093.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906132911406.png
在这里插入图片描述

面试题 03.04. 化栈为队

在这里插入图片描述

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906133302016.png

typedef struct {
int* stk;
int stkSize;
int stkCapacity;
} Stack;

Stack* stackCreate(int cpacity) {
Stack* ret = malloc(sizeof(Stack));
ret->stk = malloc(sizeof(int) * cpacity);
ret->stkSize = 0;
ret->stkCapacity = cpacity;
return ret;
}

void stackPush(Stack* obj, int x) {
obj->stk[obj->stkSize++] = x;
}

void stackPop(Stack* obj) {
obj->stkSize–;
}

int stackTop(Stack* obj) {
return obj->stk[obj->stkSize - 1];
}

bool stackEmpty(Stack* obj) {
return obj->stkSize == 0;
}

void stackFree(Stack* obj) {
free(obj->stk);
}

typedef struct {
Stack* inStack;
Stack* outStack;
} MyQueue;

MyQueue* myQueueCreate() {
MyQueue* ret = malloc(sizeof(MyQueue));
ret->inStack = stackCreate(100);
ret->outStack = stackCreate(100);
return ret;
}

void in2out(MyQueue* obj) {
while (!stackEmpty(obj->inStack)) {
stackPush(obj->outStack, stackTop(obj->inStack));
stackPop(obj->inStack);
}
}

void myQueuePush(MyQueue* obj, int x) {
stackPush(obj->inStack, x);
}

int myQueuePop(MyQueue* obj) {
if (stackEmpty(obj->outStack)) {
in2out(obj);
}
int x = stackTop(obj->outStack);
stackPop(obj->outStack);
return x;
}

int myQueuePeek(MyQueue* obj) {
if (stackEmpty(obj->outStack)) {
in2out(obj);
}
return stackTop(obj->outStack);
}

bool myQueueEmpty(MyQueue* obj) {
return stackEmpty(obj->inStack) && stackEmpty(obj->outStack);
}

void myQueueFree(MyQueue* obj) {
stackFree(obj->inStack);
stackFree(obj->outStack);
}


class MyQueue {
private:
stack inStack, outStack;

void in2out() {
while (!inStack.empty()) {
outStack.push(inStack.top());
inStack.pop();
}
}

public:
MyQueue() {}

void push(int x) {
inStack.push(x);
}

int pop() {
if (outStack.empty()) {
in2out();
}
int x = outStack.top();
outStack.pop();
return x;
}

int peek() {
if (outStack.empty()) {
in2out();
}
return outStack.top();
}

bool empty() {
return inStack.empty() && outStack.empty();
}
};

641. 设计循环双端队列

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906133456065.png

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906133533051.png

#include
#include

using namespace std;

class MyCircularDeque {

private:
vector arr;
int front;
int rear;
int capacity;

public:
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
capacity = k + 1;
arr.assign(capacity, 0);

front = 0;
rear = 0;
}

/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront(int value) {
if (isFull()) {
return false;
}
front = (front - 1 + capacity) % capacity;
arr[front] = value;
return true;
}

/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if (isFull()) {
return false;
}
arr[rear] = value;
rear = (rear + 1) % capacity;
return true;
}

/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if (isEmpty()) {
return false;
}
// front 被设计在数组的开头,所以是 +1
front = (front + 1) % capacity;
return true;
}

/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if (isEmpty()) {
return false;
}
// rear 被设计在数组的末尾,所以是 -1
rear = (rear - 1 + capacity) % capacity;
return true;
}

/** Get the front item from the deque. */
int getFront() {
if (isEmpty()) {
return -1;
}
return arr[front];
}

/** Get the last item from the deque. */
int getRear() {
if (isEmpty()) {
return -1;
}
// 当 rear 为 0 时防止数组越界
return arr[(rear - 1 + capacity) % capacity];
}

/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
return front == rear;
}

/** Checks whether the circular deque is full or not. */
bool isFull() {
// 注意:这个设计是非常经典的做法
return (rear + 1) % capacity == front;
}
};

/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque* obj = new MyCircularDeque(k);
* bool param_1 = obj->insertFront(value);
* bool param_2 = obj->insertLast(value);
* bool param_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/`

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/`

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-WsNvNMyz-1713437383459)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值