当前位置:网站首页>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 .
边栏推荐
- The difference between timer and scheduledthreadpoolexecutor
- leetcode96不同的二叉搜索树
- Which securities company is safer to open a stock account
- Regular expression collection
- Relatively easy to understand PID understanding
- PHP reads ini or env type configuration
- I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
- Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
- Windows installation WSL (II)
- Asp . Text of automatic reply to NETCORE wechat subscription number
猜你喜欢
Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
SQL数据分析之流程控制语句【if,case...when详解】
Intelligent operation and maintenance practice: banking business process and single transaction tracking
关联性——组内相关系数
export default 导出的对象,不能解构问题,和module.exports的区别
Leetcode 96 différents arbres de recherche binaires
Pytorch learning record
Asp .NetCore 微信订阅号自动回复之文本篇
Leetcode96 different binary search trees
Difficult to get up syndrome (bit by bit greed)
随机推荐
What is ThreadLocal memory leak and how to solve it
Iota in golang
SQL Server 安裝指南
Shell custom function
【QT】Qt 使用MSVC2017找不到编译器的解决办法
GCC compilation
The difference between timer and scheduledthreadpoolexecutor
实例讲解将Graph Explorer搬上JupyterLab
Windows10 install WSL (I) (wslregisterdistribution error)
Linux CentOS7安装Oracle11g的超完美新手教程
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Leetcode96 different binary search trees
Leetcode medium question sharing (5)
B tree and b+tree of MySQL
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
[cmake] cmake configuration in QT Creator
Niuke - Practice 101 - reasoning clown
Vue force cleaning browser cache
Intelligent operation and maintenance practice: banking business process and single transaction tracking
LDR6035智能蓝牙音响可对手机设备持续充放电方案