и посмотреть медиа
Golang | LeetCode
и посмотреть медиа
LeetCode tasks in Golang. Solutions with code and explanations.
LeetCode tasks in Golang. Solutions with code and explanations.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
Channel with solutions to popular LeetCode tasks in Go. Focus on optimized algorithms, efficient memory usage, and idiomatic code. Helps prepare for technical interviews in companies that use Golang.
Задача: 641. Design Circular Deque Сложность: medium Разработайте свою реализацию круговой двусторонней очереди (deque). Реализуйте класс MyCircularDeque: MyCircularDeque(int k) Инициализирует deque с максимальным размером k. boolean insertFront() Добавляет элемент в переднюю часть Deque. Возвращает true, если операция прошла успешно, или false в противном случае. boolean insertLast() Добавляет элемент в заднюю часть Deque. Возвращает true, если операция выполнена успешно, или false в противном случае. boolean deleteFront() Удаляет элемент из передней части Deque. Возвращает true, если операция прошла успешно, или false в противном случае. boolean deleteLast() Удаляет элемент из задней части Deque. Возвращает true, если операция прошла успешно, или false в противном случае. int getFront() Возвращает передний элемент из Deque. Возвращает -1, если Deque пуст. int getRear() Возвращает последний элемент из Deque. Возвращает -1, если Deque пуст. boolean isEmpty() Возвращает true, если Deque пуст, или false в противном случае. boolean isFull() Возвращает true, если Deque полон, или false в противном случае. Пример: Input ["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] [[3], [1], [2], [3], [4], [], [], [], [4], []] Output [null, true, true, true, false, 2, true, true, true, 4] 👨💻 Алгоритм: 1⃣Инициализация и проверка состояний Реализуйте конструктор для инициализации кольцевой двусторонней очереди заданного размера и методы для проверки пустоты и полноты очереди. 2⃣Операции вставки Реализуйте методы вставки элементов в переднюю и заднюю части очереди с учетом кольцевой структуры. 3⃣Операции удаления Реализуйте методы удаления элементов из передней и задней частей очереди с учетом кольцевой структуры и методы для получения переднего и заднего элементов очереди. 😎 Решение: package main type MyCircularDeque struct { deque []int front int rear int size int capacity int } func Constructor(k int) MyCircularDeque { return MyCircularDeque{ deque: make([]int, k), front: 0, rear: 0, size: 0, capacity: k, } } func (this *MyCircularDeque) InsertFront(value int) bool { if this.IsFull() { return false } this.front = (this.front - 1 + this.capacity) % this.capacity this.deque[this.front] = value this.size++ return true } func (this *MyCircularDeque) InsertLast(value int) bool { if this.IsFull() { return false } this.deque[this.rear] = value this.rear = (this.rear + 1) % this.capacity this.size++ return true } func (this *MyCircularDeque) DeleteFront() bool { if this.IsEmpty() { return false } this.front = (this.front + 1) % this.capacity this.size-- return true } func (this *MyCircularDeque) DeleteLast() bool { if this.IsEmpty() { return false } this.rear = (this.rear - 1 + this.capacity) % this.capacity this.size-- return true } func (this *MyCircularDeque) GetFront() int { if this.IsEmpty() { return -1 } return this.deque[this.front] } func (this *MyCircularDeque) GetRear() int { if this.IsEmpty() { return -1 } return this.deque[(this.rear-1+this.capacity)%this.capacity] } func (this *MyCircularDeque) IsEmpty() bool { return this.size == 0 } func (this *MyCircularDeque) IsFull() bool { return this.size == this.capacity } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!