当前位置:网站首页>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 .
边栏推荐
- 牛客-练习赛101-推理小丑
- 在证券账户上买基金安全吗?哪里可以买基金
- SQL数据分析之子查询的综合用法和案例题【耐心整理】
- The origin of usb-if Association and various interfaces
- 数据分析方法论与前人经验总结【笔记干货】
- Is it safe to choose mobile phone for stock trading account opening in Beijing?
- 使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
- 关联性——组内相关系数
- Niuke - Practice 101 - reasoning clown
- 一个实习生的CnosDB之旅
猜你喜欢

Leetcode 96 différents arbres de recherche binaires

Windows installation WSL (II)

It's nothing to be utilitarian!

What is ThreadLocal memory leak and how to solve it

SQL Server Installation Guide

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results

SQL Server 安装指南

Dongge cashes in and the boss retires?

Shell process control

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
随机推荐
heketi 记录
Is it safe and reliable to open an account in Caixue school and make new debts?
微信小程序缓存过期时间的相关设置(推荐)
Openvino model performance evaluation tool DL workbench
I want to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
【CMake】Qt creator 里面的 cmake 配置
ERP项目施行计划的目的是什么?
Practical calculation of the whole process of operational amplifier hysteresis comparator
Is the securities account given by qiniu business school safe? Where can I open an account
Vue force cleaning browser cache
Shell custom function
基于全志H3的QT5.12.9移植教程
The difference between timer and scheduledthreadpoolexecutor
leetcode96不同的二叉搜索樹
Three methods of finding inverse numbers
使用htaccess文件禁止目录里的脚本执行权限
Dongge cashes in and the boss retires?
What is the purpose of ERP project implementation plan?
Is it safe for qiniu college to open an account? How to open an account?
Shell process control