当前位置:网站首页>Sword finger offer 09 Implementing queues with two stacks
Sword finger offer 09 Implementing queues with two stacks
2022-07-05 08:21:00 【Programmer Xiao Li】
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Example 1:
Input :
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
Output :[null,null,3,-1]
Example 2:
Input :
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
Output :[null,-1,null,null,5,2]
Tips :
1 <= values <= 10000
At most appendTail、deleteHead Conduct 10000 Secondary call
Ideas :
1. An insert stack , An output stack . When inserting data , Write into the insert stack .
2. When outputting data , Pop up from the output stack first , If the output stack does not exist , Then press... From the input stack in turn .
class CQueue {
Stack<Integer> pushStack = new Stack<Integer>();
Stack<Integer> popStack = new Stack<Integer>();
public CQueue() {
}
public void appendTail(int value) {
pushStack.push(value);
}
public int deleteHead() {
if (popStack.empty()){
while (!pushStack.empty()){
popStack.push(pushStack.pop());
}
}
if (popStack.empty()){
return -1;
}
return popStack.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
边栏推荐
- Basic embedded concepts
- OLED 0.96 inch test
- 实例009:暂停一秒输出
- Some thoughts on extracting perspectives from ealfa and Ebeta
- The firmware of the connected j-link does not support the following memory access
- What are the test items of power battery ul2580
- 实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
- Network port usage
- Imx6ull bare metal development learning 1-assembly lit LED
- Count the number of inputs (C language)
猜你喜欢

Shell script

Stablq of linked list
![[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement](/img/54/78e4e10724b2762274b06debb10877.jpg)
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement

Various types of questions judged by prime numbers within 100 (C language)

PMSM dead time compensation

Management and use of DokuWiki

My-basic application 2: my-basic installation and operation

QEMU STM32 vscode debugging environment configuration

Talk about the function of magnetic beads in circuits

Introduction of air gap, etc
随机推荐
Synchronization of QT multithreading
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
My-basic application 2: my-basic installation and operation
Weidongshan Internet of things learning lesson 1
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
实例006:斐波那契数列
Explain task scheduling based on Cortex-M3 in detail (Part 1)
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Wifi-802.11 negotiation rate table
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
Zero length array in GNU C
Charge pump boost principle - this article will give you a simple understanding
Explication de la procédure stockée pour SQL Server
Ble encryption details
Management and use of DokuWiki (supplementary)
Hardware 3 -- function of voltage follower
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
Management and use of DokuWiki
STM32 --- NVIC interrupt