当前位置:网站首页>Leetcode skimming: stack and queue 02 (realizing stack with queue)
Leetcode skimming: stack and queue 02 (realizing stack with queue)
2022-07-02 00:26:00 【Taotao can't learn English】
225. Using queue to implement stack
Use queues to implement the following operations of the stack :
- push(x) – Elements x Push
- pop() – Remove the top element
- top() – Get stack top element
- empty() – Whether the return stack is empty
Be careful :
- You can only use the basic operations of the queue -- That is to say push to back, peek/pop from front, size, and is empty These operations are legal .
- The language you use may not support queues . You can use list perhaps deque( deque ) To simulate a queue , As long as it's a standard queue operation .
- You can assume that all operations are valid ( for example , No call to an empty stack pop perhaps top operation ).
For this question , First of all, we need to know several methods
- pop Stack exit element
- push Put elements at the end of the stack
- peek Look at the top of the stack
- offer Team tail in element
- poll The first element of the team
- peek View queue elements
I use it ArrayDeque Simulated . Actually check API This class has many duplicate methods .
boolean add(E e)
Inserts the specified element at the end of this double ended queue .
void addFirst(E e)
Inserts the specified element at the beginning of this double ended queue .
void addLast(E e)
Inserts the specified element at the end of this double ended queue .
void clear()
Remove all elements from this double ended queue .
ArrayDeque clone()
Return a copy of this double ended queue .
boolean contains(Object o)
If this double ended queue contains the specified element , Then return to true.
Iterator descendingIterator()
Returns an iterator that iterates over the elements of this two-way queue in reverse order .
E element()
obtain , But do not remove the head of the queue represented by this double ended queue .
E getFirst()
obtain , But do not remove the first element of this double ended queue .
E getLast()
obtain , But do not remove the last element of this double ended queue .
boolean isEmpty()
If this double ended queue does not contain any elements , Then return to true.
Iterator iterator()
Returns the iterator that iterates over the elements of this double ended queue .
boolean offer(E e)
Inserts the specified element at the end of this double ended queue .
boolean offerFirst(E e)
Inserts the specified element at the beginning of this double ended queue .
boolean offerLast(E e)
Inserts the specified element at the end of this double ended queue .
E peek()
obtain , But do not remove the head of the queue represented by this double ended queue ; If this double ended queue is empty , Then return to null.
E peekFirst()
obtain , But do not remove the first element of this double ended queue ; If this double ended queue is empty , Then return to null.
E peekLast()
obtain , But do not remove the last element of this double ended queue ; If this double ended queue is empty , Then return to null.
E poll()
Get and remove the header of the queue represented by this double ended queue ( let me put it another way , The first element of this two-way queue ); If this double ended queue is empty , Then return to null.
E pollFirst()
Gets and removes the first element of this double ended queue ; If this double ended queue is empty , Then return to null.
E pollLast()
Get and remove the last element of this two-way queue ; If this double ended queue is empty , Then return to null.
E pop()
Pop an element from the stack represented by this double ended queue .
void push(E e)
Push the element into the stack represented by this double ended queue .
E remove()
Get and remove the header of the queue represented by this double ended queue .
boolean remove(Object o)
Remove a single instance of the specified element from this double ended queue .
E removeFirst()
Gets and removes the first element of this double ended queue .
boolean removeFirstOccurrence(Object o)
Remove the specified element that first appears in this double ended queue ( When traversing a double ended queue from head to tail ).
E removeLast()
Get and remove the last element of this two-way queue .
boolean removeLastOccurrence(Object o)
Remove the last occurrence of the specified element in this double ended queue ( When traversing a double ended queue from head to tail ).
int size()
Returns the number of elements in this double ended queue .
Object[] toArray()
Returns an array containing all the elements of this double ended queue in the proper order ( From the first element to the last element ).
T[]
toArray(T[] a)
Returns an array containing all the elements of this double ended queue in the proper order ( From the first element to the last element ); The runtime type of the returned array is the runtime type of the specified array .
package com.programmercarl.stacks_queues;
import java.util.ArrayDeque;
/** * @ClassName MyStack * @Descriotion TODO * @Author nitaotao * @Date 2022/6/29 12:10 * @Version 1.0 * https://leetcode.cn/problems/implement-stack-using-queues/ * 225. Using queue to implement stack * Ideas : * Stack First in, then out Can only A End entry ,A hold sth. level with both hands * queue fifo Can only A End entry ,B hold sth. level with both hands * pop Stack exit element * push Put elements at the end of the stack * peek Look at the top of the stack * * offer Team tail in element * poll The first element of the team * peek View queue elements * * **/
public class MyStack {
// Source queue
private ArrayDeque queueOrigin;
// Copy queue
private ArrayDeque queueCopy;
public MyStack() {
queueOrigin = new ArrayDeque();
queueCopy = new ArrayDeque();
}
/** * Push an element * @param x */
public void push(int x) {
// Push data at the end of the source queue
queueOrigin.offer(x);
}
/** * Launch an element * @return */
public int pop() {
/** * The source queue removes The last element , All other elements are pushed into the replication queue */
int lenOrigin = queueOrigin.size();
while (lenOrigin > 1) {
queueCopy.offer(queueOrigin.poll());
lenOrigin--;
}
// Get the value to be pushed
int result = (int) queueOrigin.poll();
/** * Copy the queue and push all elements into the original queue */
int lenCopy = queueCopy.size();
while (lenCopy > 0) {
queueOrigin.offer(queueCopy.poll());
lenCopy--;
}
return result;
}
/** * View top element * @return */
public int top() {
/** * The source queue removes The last element , All other elements are pushed into the replication queue */
int lenOrigin = queueOrigin.size();
while (lenOrigin > 1) {
queueCopy.offer(queueOrigin.poll());
lenOrigin--;
}
// Get the value to be pushed
int result = (int) queueOrigin.peek();
queueCopy.offer(queueOrigin.poll());
/** * Copy the queue and push all elements into the original queue */
int lenCopy = queueCopy.size();
while (lenCopy > 0) {
queueOrigin.offer(queueCopy.poll());
lenCopy--;
}
return result;
}
/** * Sentenced to empty * @return */
public boolean empty() {
return queueOrigin.size() == 0;
}
}

In fact, it's not easy to think , After all, every time you use elements, you have to put all the elements in another place , Take this element again . This kind of inefficient and farting thing is unnecessary in reality . Code is not hard , There are notes , Just look for yourself .
边栏推荐
- 【模板】自适应辛普森积分
- js 公共库 cdn 推荐
- 【QT】对于Qt MSVC 2017无法编译的问题解决
- SQL数据分析之子查询的综合用法和案例题【耐心整理】
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- The difference between timer and scheduledthreadpoolexecutor
- 2022拼多多详情/拼多多商品详情/拼多多sku详情
- Is it safe and reliable to open an account in Caixue school and make new debts?
- [template] adaptive Simpson integral
- 启牛学院开户安全的吗?开户怎么开?
猜你喜欢

如何提升数据质量

Asp .NetCore 微信订阅号自动回复之文本篇

SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
![[cmake] cmake configuration in QT Creator](/img/e3/1cf76f88eaddb5d32184523dfb049c.png)
[cmake] cmake configuration in QT Creator

【QT】對於Qt MSVC 2017無法編譯的問題解决

The origin of usb-if Association and various interfaces

数据分析方法论与前人经验总结【笔记干货】

Review data desensitization system

Shell process control

Example explanation: move graph explorer to jupyterlab
随机推荐
Correlation - intra group correlation coefficient
Database -- sqlserver details
Download the online video m3u8 tutorial
[QT] QT cannot find a solution to the compiler using msvc2017
北京炒股开户选择手机办理安全吗?
Windows installation WSL (II)
数据库--SqlServer详解
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
An intern's journey to cnosdb
【CMake】Qt creator 里面的 cmake 配置
PHP reads ini or env type configuration
S32Kxxx bootloader之UDS bootloader
Leetcode 96 différents arbres de recherche binaires
启牛商学院给的证券账户安不安全?哪里可以开户
Graduation season is both a farewell and a new beginning
The difference between timer and scheduledthreadpoolexecutor
使用htaccess文件禁止目录里的脚本执行权限
Jielizhi Bluetooth headset quality control and production skills [chapter]
LeetCode中等题题分享(5)
【QT】對於Qt MSVC 2017無法編譯的問題解决