当前位置:网站首页>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();
*/
边栏推荐
- Hardware 1 -- relationship between gain and magnification
- C WinForm [view status bar -- statusstrip] - Practice 2
- On boost circuit
- Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
- Explain task scheduling based on Cortex-M3 in detail (Part 1)
- STM32 --- GPIO configuration & GPIO related library functions
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- Carrier period, electrical speed, carrier period variation
- Basic embedded concepts
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
猜你喜欢
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
Network communication process
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
MySQL MHA high availability cluster
STM32 --- serial port communication
How to select conductive slip ring
Stm32--- systick timer
STM32---IIC
Introduction of air gap, etc
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
随机推荐
亿学学堂给的证券账户安不安全?哪里可以开户
Take you to understand the working principle of lithium battery protection board
Connection mode - bridge and net
matlab timeserise
MySQL之MHA高可用集群
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Semiconductor devices (I) PN junction
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Adaptive filter
Naming rules for FreeRTOS
实例006:斐波那契数列
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
Weidongshan Internet of things learning lesson 1
如何写Cover Letter?
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Introduction of air gap, etc
WiFi wpa_ Detailed description of supplicant hostpad interface
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl