当前位置:网站首页>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();
*/
边栏推荐
- What are the test items of power battery ul2580
- STM32 --- serial port communication
- List of linked lists
- Soem EtherCAT source code analysis II (list of known configuration information)
- Explication de la procédure stockée pour SQL Server
- matlab timeserise
- STM32 --- configuration of external interrupt
- VESC Benjamin test motor parameters
- Zero length array in GNU C
- Network port usage
猜你喜欢
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
STM32 tutorial triple ADC interleaved sampling
Take you to understand the working principle of lithium battery protection board
The firmware of the connected j-link does not support the following memory access
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
C WinForm [view status bar -- statusstrip] - Practice 2
实例006:斐波那契数列
STM32---IIC
随机推荐
Design a clock frequency division circuit that can be switched arbitrarily
PMSM dead time compensation
Weidongshan Internet of things learning lesson 1
What are the test items of power battery ul2580
Verilog -- state machine coding method
Soem EtherCAT source code analysis I (data type definition)
Problem solving: interpreter error: no file or directory
Sql Server的存儲過程詳解
Halcon's practice based on shape template matching [1]
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
NTC thermistor application - temperature measurement
STM32 single chip microcomputer -- volatile keyword
STM32 --- GPIO configuration & GPIO related library functions
MySQL之MHA高可用集群
OC and OD gate circuit
STM32 tutorial triple ADC interleaved sampling
亿学学堂给的证券账户安不安全?哪里可以开户
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Connection mode - bridge and net